Spring 注解 整理

 首先 在xml中配置

xmlns:context="http://www.springframework.org/schema/context"

http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">


<context:annotation-config />
<context:component-scan base-package="com.bjsxt"/>

然后再要加入的

@Component/ @Service/ @Controller/ @Repository(value = "articleManager")//方法名首字母小写

 

哪里有调用 或者实例化 就在那个方法前加

@Controller

@Resource

例如

@Repository (value = "articleDAO")

public class ArticleDAOImpl implements ArticleDAO{}

然后再哪里要注入就在哪里写

@Controller
public class ArticleManagerImpl implements ArticleManager{

@Resource
private ArticleDAO articleDAO;

@Override
public ArticleDTO getSelectComment(int userId) {}

  1. @Resource(重要)

a)     加入:j2ee/common-annotations.jar

b)     默认按名称,名称找不到,按类型

c)     可以指定特定名称

d)     推荐使用

e)     不足:如果没有源码,就无法运用annotation,只能使用xml

  1. @Component @Service @Controller @Repository

a)     初始化的名字默认为类名首字母小写

b)     可以指定初始化bean的名字

  1. @Component @Service @Controller @Repository

a)     初始化的名字默认为类名首字母小写

b)     可以指定初始化bean的名字

  还有一种方法是注入    

直接在bean.xml中写

<beans>
<bean id="u" class="com.bjsxt.dao.impl.UserDAOImpl" >
</bean>
<bean id="userService" class="com.bjsxt.service.UserService" >
<property name="userDAO" bean="u"/>
</bean>
</beans>

在xml中实例化userServiice

然后再那里用到就在哪里直接调用

原文地址:https://www.cnblogs.com/daniell003/p/3322675.html