一般版本低的JDK在用到@Resource注解时,可能需要引入j2ee/common-annotations.jar。我用的是JDK1.6,没需要引入。
@Component大致可以分为以下几个注解:
a.@controller控制器(注入服务),用于标注控制层组件(如struts中的action)
b.@service服务(注入dao),用于标注业务层组件
c.@repository dao(实现dao访问),用于标注数据访问组件,即DAO组件
d.@component (把普通pojo实例化到spring容器中,相当于配置文件中的<bean id="" class=""/>),泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注
@Component,@Service,@Controller,@Repository注解的类,并把这些类纳入进spring容器中管理。
在annotaion配置注解中用@Component来表示一个通用注释用于说明一个类是一个spring容器管理的类。即就是该类已经拉入到spring的管理中了。而@Controller, @Service, @Repository是@Component的细化,这三个注解比@Component带有更多的语义,它们分别对应了控制层、服务层、持久层的类。
@Repository标签是用来给持久层的类定义一个名字,让Spring根据这个名字关联到这个类。
例如:
@Repository("userDao") public class UserDaoImpl implements UserDao{ ........................................ }
声明了UserDaoImpl 在Spring容器中叫userDao这个名字。
@Service是用于服务层的IServiceImpl类文件,功能与@Repository类似。
另外标签:@Autowired 用来注入。
例如:
@Autowired private UserDao userDao;
这样就注入进去了,相当于我们new了个实现类,我们就无需写setter方法了。
下面这个XML代码段是引入component的扫描组件(想要实现组件方式扫描,必须在XML中配置下面代码):
<context:component-scan base-package="com.bijian.study" />
其中base-package为需要扫描的包(含所有子包)
如果只需扫描指定包下的特定的包,可以进一步配置,如下所示:
<context:component-scan base-package="com.bijian.study"> <context:include-filter type="regex" expression=".*DaoImpl"/> <context:include-filter type="regex" expression=".*ServiceImpl"/> </context:component-scan>
实例:
IUserService.java
package com.bijian.study.service; import com.bijian.study.model.User; public interface IUserService { public void add(User user) ; }
UserServiceImpl.java
package com.bijian.study.service.impl; import javax.annotation.Resource; import org.springframework.stereotype.Service; import com.bijian.study.dao.IUserDAO; import com.bijian.study.model.User; import com.bijian.study.service.IUserService; /** * 使用@Component实现把普通类实例化到spring容器中,相当于配置文件中的<bean id="" class=""/> * Component自动实例化的bean名为类的首字母小写,即userDAOImpl。如果想指定固定名可以写成@Component("userService") * 当此处用@Component("userService")进行重命名后,在Test.java中访问Bean时需要用userService否则用userServiceImpl。 * */ //@Component("userService") @Service("userService") //@Service("userService") @Scope("prototype") public class UserServiceImpl implements IUserService { // 为此属性提供setter,getter方法.是为了让Spring容器管理 //@Autowired @Resource(name="userDAO") private IUserDAO userDAO ; public IUserDAO getUserDAO() { return userDAO; } /** * 用Annotation的Resource方法自动装载 * 用@Resource(name="userDAO")指定想要装载的bean名 * @param userDAO */ //@Resource(name="userDAO") public void setUserDAO(IUserDAO userDAO) { this.userDAO = userDAO; } @Override public void add(User user) { userDAO.save(user); } }
getBean的默认名称是类名(头字母小写),如果想自定义,可以@Service("***")这样来指定,这种bean默认是单例的,如果想改变,可以使用@Service("beanName") @Scope("prototype")来改变。
IUserDAO.java
package com.bijian.study.dao; import com.bijian.study.model.User; public interface IUserDAO { public void save(User user) ; }
UserDAOImpl.java
package com.bijian.study.dao.impl; import javax.annotation.Resource; import org.springframework.stereotype.Component; import org.springframework.stereotype.Repository; import com.bijian.study.dao.IUserDAO; import com.bijian.study.model.User; /** * 使用@Component实现把普通类实例化到spring容器中,相当于配置文件中的<bean id="" class=""/> * Component自动实例化的bean名为类的首字母小写,即userDAOImpl。如果想指定固定名可以写成@Component("userDAO") * 当此处用@Component("userDAO")进行重命名后,需要在用到此Bean的源代码(UserServiceImpl.setUserDAO)处使用@Resource(name="userDAO")注入。 */ //@Component("userDAO") @Repository("userDAO") public class UserDAOImpl implements IUserDAO { @Override public void save(User user) { System.out.println("user save success..."); System.out.println(user.toString()); } }
User.java
package com.bijian.study.model; public class User { private String username ; private String password ; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String toString() { return "User [username=" + username + ", password=" + password + "]"; } }
beans.xml
<?xml version="1.0" encoding="UTF-8"?> <!-- spring2.5配置文件固定写法 --> <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-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <!-- 使用此配置就可以在源码中写Annotation(注解),注解编程 --> <context:annotation-config /> <!-- 使用此配置代码,spring会自动扫描com.yusj包下的所有带@component注解的Class文件 @Component包括:@controller,@service,@repository和@component 当分不清楚Class具体要做什么工作时,可以统一写成@component. @controller:一般写在控制层。 @service:一般写在服务层。 @repository:一般写在持久层,也就是DAO。 @component (把普通pojo实例化到spring容器中,相当于配置文件中的<bean id="" class=""/> --> <context:component-scan base-package="com.bijian.study" /> </beans>
Test.java
package com.bijian.study.spring; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.bijian.study.model.User; import com.bijian.study.service.IUserService; public class Test { public static void main(String[] args) { /** * Spring提供的读取配置文件方法,此处推荐使用ApplicationContext而非BeanFactory. * beans配置文件默认读取src根目录文件名相同的XML文件 * 如果需要放在特殊的位置,那么需要指定具体路径,比如:com/bijian/xml/beans.xml */ ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); /** * 获取UserServiceImpl.java中利用@Component("userService")自动装载的bean * */ IUserService service = (IUserService) ctx.getBean("userService"); // 初始化用户并赋值 User user = new User(); user.setUsername("张三"); user.setPassword("zhangsan"); // 添加用户测试 service.add(user); /** * 输出结果: * user save success... * User [username=张三, password=zhangsan] */ } }
参考资料:
http://www.cnblogs.com/chenzhao/archive/2012/02/25/2367978.html
http://ysj5125094.iteye.com/blog/2054421
相关推荐
Spring注解@Component、@Repository、@Service、@Controller区别.doc
Spring注解 @Component、@Repository、@Service、@Controller区别,有兴趣的可能看一下。
凡带有@Component,@Controller,@Service,@Repository 标志的等于告诉Spring这类将自动产生对象,而@Resource则等于XML配置中的ref,告诉spring此处需要注入对象,所以用@Resource就有了ref的功效。 要用注解注入方式...
项目分层之后(引入dao,service,web层之后), @Component注解还有三个分身---@repository ,@Service,@Controller。这三个注解怎么用,以后再说,目前都使用@Component。 1.1 为Car类加@Component注解 注解也是要用...
在这份文档中,我们深入探讨了Spring的核心注解,包括但不限于@Component、@Repository、@Service、@Controller和@Autowired。这些注解简化了配置过程,减少了样板代码,并使得组件之间的耦合度降低,更有利于单元...
面试遇到的问题 1.spring的AOP/IOC怎么用 Ioc: ...Aop: ... 2.设计模式 单例模式 ...@Controller ...@Repository 对Dao实现类进行注解 (特殊的@Component) @Service 用于对业务逻辑层进行注解, (特殊的@Compone
Spring @讲义.txt Spring注解@Component、@Repository、@Service、@Controller区别
注解包含: 拦截器 , 过滤器 , 序列化 , @After , @AfterReturning , @AfterThrowing , @annotation , @Around , @Aspect , @Autowired , @Bean , @Before , @Component , @ComponentScan , @ComponentScans , @...
1 @Controller 2 @Service 3 @Repository 4 @Component 5 @Bean 6 @Configuration 三、 注入相关 1 @Resource 2 @Autowired 3 @Qualifier 4 @value 5 @ConfigurationProperties 四、 HTTP请求相关 1 @GetMapping 2 @...
SSH笔记-annotation配置注入关系(使用@Component、@Service、@Repository、@Controller)
本文将详细介绍Spring Boot中最常用的注解,包括@SpringBootApplication、@Component、@Service、@Repository、@Controller、@RequestMapping、@GetMapping、@PostMapping、@PutMapping、@DeleteMapping、@Autowired...
spring注解开发@PreDestroy除了@Component外,Spring提供了3个功能基本和@Component等效的注解 @Repository 用于对DAO实现类进行标注 @Service 用于对Service实现类进行标注 @Controller 用于对Controller实现类进行...
- @Component @Controller @Service @Repository 区别? <!-- /TOC --> Spring MVC常见知识点及源码解析 MVC 是什么 / 有什么优点? MVC是一种设计模式,遵循 模型(Model),视图(View) 和 控制器(Controller)的...
• @Controller • @Service • @Autowired • @RequestMapping • @RequestParam • @ModelAttribute • @Cacheable • @CacheFlush • @Resource • @PostConstruct • @PreDestroy • @Repository ...
@Service @Component @Repository @Controller @Bean : 用在方法上面,将方法的返回值装配成Bean 2、AOP的具体应用 3、MVC 4、RESTfull 5、DAO层 MySQL、MyBatis NoSQL 事务 6、拦截器 7、...
Spring组件自动扫描详解及实例代码 ...其他特定的注解有@Repository、@Service和@Controller,它们分别标识了持久层、服务处和表现层的组件。 实现方法 User.Java package com.zzj.bean; impor
@Service: 一般用来修饰 业务service层 @Repository: 一般用来修饰 数据访问dao层 @Component: 当一个类 , 分不清是 哪个层 可以用这个注解来修饰 @Controller: 一般用来修饰 控制层 @Autowired @Qualifier ...
依赖注入(DI)的好处不再赘言,使用过spring框架的都知道。...比如spring中,服务的注册是通过xml配置文件的标签或是注解@Repository、@Service、@Controller、@Component实现的;对象的获取可以Applicati
包扫描@ComponentScan+组件标注注解(@Controller/@Service/@Repository/@Component)一般作用与自己写的类。 includeFilters表示包扫描的条件,起到过滤的作用。默认@ComponentScan会扫描标注这四个注解,将标注这...