Spring 处理使用XML配置文件进行依赖注入外, 还可以使用注解依赖注入
1. 在springXML中加入spring-context-2.5.xsd 和 <context:annotation-config/>
<?xml version="1.0" encoding="UTF-8"?>
<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">
<context:annotation-config/>
<bean id="personDaoxxxx" class="cn.itcast.dao.impl.PersonDaoBean"></bean>
<bean id="personService" class="cn.itcast.service.impl.PersonServiceBean">
<!-- 使用XML配置文件进行依赖注入
<constructor-arg index="0" type="cn.itcast.dao.PersonDao" ref="personDao"/>
<constructor-arg index="1" value="传智播客"/>
-->
</bean>
</beans>
<context:annotation-config/>这个配置隐式注册了多个对注释进行解析处理的处理器。
2.在Java代码中使用@Autowired 或 @Resource注解
@Autowired Spring定义的注解,默认按类型装配
@Resource Javax下的注解,默认按名称装配,当找不到名称时按类型装配,推荐使用。
public class PersonServiceBean implements PersonService {
@Resource //作用于属性
private PersonDao personDao;
private String name;
@Resource(name="personDao") //作用于方法
public void setPersonDao(PersonDao personDao) {
this.personDao = personDao;
}
}
3.Spring2.5中引入对bean类文件扫描的注解, 它可以在类路径下寻找标注了@Component、@Service、@Controller、@Repository注解的类,并把他们纳入spring容器中管理。它的作用和在xml中使用bean节点配置组件是一样的。要使用扫描机制,需在spring配置文件中使用:
<?xml version="1.0" encoding="UTF-8"?>
<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">
<!-- scan all beans and inject dependence -->
<context:component-scan base-package="com.myproject"/>
</beans>
它包含了1.中<context:annotation-config/>的功能,因此不必重复添加。
@Controller 注解Controller层
@Service 注解Service层
@Repository 注解Dao层
@Component 注解其它层
另外还可以使用一下注解
@Scope 标明singleton 或 prototype
@PostConstruct 标明初始化init()方法
@PreDestroy 标明销毁方法
@Service
public class AccountService {
@Resource
private AccountDao accountDao;
public Account Login(String username, String password){
Account account = new Account();
account.setUsername(username);
account.setPassword(password);
List<Account> list= accountDao.queryAccount(account);
if(list.size()==1)
return list.get(0);
else
return null;
}
public void reqister(Account account){
accountDao.insertAccount(account);
}
}
分享到:
相关推荐
spring依赖注入底层详解,很不错的资源,欢迎大家来下载学习。
javaEE 开发中 现在最成熟的框架之一应该就是spring了 spring框架最强大的地方就是实现了依赖注入 也叫控制反转 最近的一个项目中用的就是 spring框架 spring框架是工厂模式的应用结合了MVC的设计思想 大家可以...
该资源包含了一个利用spring实现的依赖注入的案例,简单易懂。
java 无需数据库 简单实例 如何实现Spring依赖注入 AOP
Spring依赖注入检查,适合初学者进一步了解Spring框架。
spring 依赖注入三种方式测试源码,测试细节参考博文http://blog.csdn.net/u010679383/article/details/71305400
Spring依赖注入使用构造设注入demo。完整可运行。
模仿Spring依赖注入,代码详细,简单,明了
NULL 博文链接:https://zhangyulong.iteye.com/blog/856986
详细阐明spring依赖注入工作原理,基于注解的一个完整例子,当然数据库需要自己去创建
揭秘spring核心机制, 模拟spring依赖注入
NULL 博文链接:https://matchless1688.iteye.com/blog/1038302
Spring依赖注入使用实例工厂设注入demo。完整可运行。
NULL 博文链接:https://huangminwen.iteye.com/blog/1041743
Spring依赖注入使用setter设注入demo。完整可运行。
Spring依赖注入使用静态工厂设注入demo。完整可运行。
Spring依赖注入DI.zip
Spring依赖注入——java项目中使用spring注解方式进行注入.rar
NULL 博文链接:https://coolszy.iteye.com/blog/512193