spring @Component、@Repository、@Controller、@Service、@Configuration的使用
2018-01-16 17:39
393 查看
一、组件扫描(component scan)
<context:component-scan base-package="packagename" />
在配置文件加上此标签后,spring将在packagename包下扫描Java类,如果扫描到有@Component、@Repository、@Controller、@Service等注解的类,将此类注册为Bean。
@Service用于标注业务层组件
@Controller用于标注控制层组件
@Repository用于标注数据访问组件,即DAO组件
@Component泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:component-scan base-package="com.spring.annotation.more" /> </beans>
利用@Component、@Repository、@Controller、@Service减少在xml的Bean的配置,这类注解默认value值为类名首字母小写。注解的value中相当于xml配置文件中Bean的id值。
1.Student类上加@Component注解,相当于在xml中配置Bean为
<bean id="student" class="com.spring.annotation.more.Student"/>
@Component public class Student { @Value(value="10") private int id; @Value(value="lisi") private String name; //set、get方法 }
2.DaoImpl类上加@Repository(value=”dao”)注解,相当于在xml中配置Bean为
<bean id="dao" class="com.spring.annotation.more.DaoImpl"/>
@Repository(value="dao") public class DaoImpl implements IDao{ @Resource private Student student; // 得到学生的信息 @Override public String getStuInfo() { return "id:"+student.getId() + " name:" + student.getName(); } //可不写set、get,因其用字段注入,不是从set方法注入 }
3.ServiceImpl类上加@Service(value=”service”)注解,相当于在xml中配置Bean为
<bean id="service" class="com.spring.annotation.more.ServiceImpl"/>
@Service(value="service") public class ServiceImpl implements IService{ @Resource(name="dao") private IDao dao; @Override public void printStuInfo() { System.out.println(dao.getStuInfo()); } }
4.运行测试
public class MyMain { public static void main(String[] args) { //从xml配置文件得到上下文 AbstractApplicationContext context = new ClassPathXmlApplicationContext("appContextAnnotatMore.xml"); IService service = (IService)context.getBean("service"); service.printStuInfo(); } }
二、自动装配(autowiring)
自动装配是spring自动满足bean之间的依赖。例如:<bean id="dao" class="com.springxml.DaoImpl"> <property name="student" ref="student"/> </bean>
Dao Bean依赖Student Bean,则可以用@Autowired或@Resource注解在java代码中添加依赖。
原理参看:
Spring使用@Autowired,@Qualifier,Resource注解配置bean
三、java配置类
经过一和二,xml配置文件只用配置<context:component-scan>包扫描标签就行。若不想配置xml,实现完全java类配置,则用java配置类用@Configuration注解。
@Configuration注解表明某个类是配置类,例如:
@ComponentScan("com.spring.annotation.more") @Configuration public class MyContext { /* @Bean public Student student() { return new Student(); } @Bean public IDao dao() { return new DaoImpl(); } @Bean public IService service(){ return new ServiceImpl(); }*/ }
@ComponentScan(“com.spring.annotation.more”)注解扫描com.spring.annotation.more包,相当于
<context:component-scan base-package="com.spring.annotation.more" />,@Configuration表示这是一个java配置类。
对于@Bean注解:
@Bean public IDao dao() { return new DaoImpl(); }
相当于
<bean id="service" class="com.spring.annotation.more.ServiceImpl"/>,当然如果已经使用@Sevice注解配置此Bean,就不需要再java配置类配置Bean。
运行测试:
public class MyMain { public static void main(String[] args) { //从java配置类得到上下文 AbstractApplicationContext context = new AnnotationConfigApplicationContext(MyContext.class); IService service = (IService) context.getBean("service"); service.printStuInfo(); } }
源码:
spring零配置Bean示例源码spring零配置Bean示例源码
相关文章推荐
- Spring学习使用标签来标记资源(@Component、@Repository、 @Service和@Controller)和用法(包括如何jsp正在使用)
- Spring注解@Component、@Repository、@Service、@Controller使用
- AAA Spring注解@Component、@Repository、@Service、@Controller及scope区别
- Spring注解的使用和区别:@Component、@Service、@Repository、@Controller
- Spring注解@Component、@Repository、@Service、@Controller区别
- (转载)Spring 注解@Component,@Service,@Controller,@Repository
- (转载)Spring 注解@Component,@Service,@Controller,@Repository
- (转载)Spring 注解@Component,@Service,@Controller,@Repository
- spring部分注解@Repository,@Service,@Controller,@Component,@scope,@Resource,@Autowired
- Spring 注解@Component,@Service,@Controller,@Repository
- Spring 注解@Component,@Service,@Controller,@Repository
- Spring注入方式及用到的注解 -----@Component,@Service,@Controller,@Repository
- Spring代码实例系列-06:通过注解@Component、@Controller、@Service和@Repository定义bean,@Autowire装配bean
- Spring 注解@Component,@Service,@Controller,@Repository
- 使用 @Repository、@Service、@Controller 和 @Component 将类标识为 Bean
- Spring 注解@Component,@Service,@Controller,@Repository
- Spring注入方式及用到的注解 -----@Component,@Service,@Controller,@Repository
- Spring注解@Component、@Repository、@Service、@Controller区别
- Spring注解@AutoWired @Component、@Repository、@Service、@Controller区别
- Spring注解的使用和区别:@Component、@Service、@Repository、@Controller