spring-04 之IOC 属性注入(注解方式 附代码)
2018-02-05 16:23
781 查看
切记:不骄不躁 不浮不沉 学会思考
简介:上一篇文章介绍了 spring中通过配置文件注入属性的方式 这篇将介绍另外一种方式 :基于注解的属性注入
进行属性注入 有两个注解 分别是@Autowired @Resource
@Autowired :自动注入。默认按类型装配,它可以对类成员变量、方法及构造函数进行标注,完成自动装配的工作。 通过 使用@Autowired来消除 set ,get方法,即使用@Autowiredjava bean中不需要get、set方法。
@Resource:默认按名称装配名称可以通过name属性进行指定,如果没有指定name属性,当注解写在字段上时,默认取字段名,按照名称查找,如果注解写在setter方法上默认取属性名进行装配,如果name属性一旦指定,就只会按照名称进行装配。
@Autowired@Resource的区别 可参考https://www.zhihu.com/question/39356740
第一步:创建web工程 导入对应的java的jar包和spring的jar包
第二步:创建applicationContext.xml 引入约束
package per.spring.dao;
import org.springframework.stereotype.Component;
@Component(value="bookDao")
public class BookDao {
public void add() {
System.out.println("bookDao.........");
}
}
第四步:class里面 加入测试方法 使用@Autowired 和@Resource分别注入属性 ;配置xml 开启注解扫描
java代码:
<?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:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 注解方式 实现bean的创建 和属性注入 -->
<!-- 开启注解扫描 扫描包里面的类、方法、属性是否有注解 要扫描多个包 可用逗号隔开 这里扫描了三个包 -->
<context:component-scan base-package="per.spring.beans,per.spring.service,per.spring.dao"></context:component-scan>
<!-- 装配bean -->
<bean id="bookService" class="per.spring.service.BookService"></bean>
</beans>运行结果:
配置文件与注解方式混合使用: 用配置文件方式创建bean 用注解方式注入属性
简介:上一篇文章介绍了 spring中通过配置文件注入属性的方式 这篇将介绍另外一种方式 :基于注解的属性注入
进行属性注入 有两个注解 分别是@Autowired @Resource
@Autowired :自动注入。默认按类型装配,它可以对类成员变量、方法及构造函数进行标注,完成自动装配的工作。 通过 使用@Autowired来消除 set ,get方法,即使用@Autowiredjava bean中不需要get、set方法。
@Resource:默认按名称装配名称可以通过name属性进行指定,如果没有指定name属性,当注解写在字段上时,默认取字段名,按照名称查找,如果注解写在setter方法上默认取属性名进行装配,如果name属性一旦指定,就只会按照名称进行装配。
@Autowired@Resource的区别 可参考https://www.zhihu.com/question/39356740
第一步:创建web工程 导入对应的java的jar包和spring的jar包
第二步:创建applicationContext.xml 引入约束
<?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:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xmlns:util="http://www.springframework.org/schema/util" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> </beans>第三步:创建类 创建方法
package per.spring.service; import javax.annotation.Resource; import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.stereotype.Component; import per.spring.dao.BookDao; public class BookService { //对象类型属性 BookDao为一个class //使用注解方式注入 不需要get、set方法 只需要加入注解 private BookDao bookDao; private BookDao bookDao2; public void add() { System.out.println("service......"); } }
package per.spring.dao;
import org.springframework.stereotype.Component;
@Component(value="bookDao")
public class BookDao {
public void add() {
System.out.println("bookDao.........");
}
}
第四步:class里面 加入测试方法 使用@Autowired 和@Resource分别注入属性 ;配置xml 开启注解扫描
java代码:
package per.spring.service; import javax.annotation.Resource; import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.stereotype.Component; import per.spring.dao.BookDao; public class BookService { //对象类型属性 BookDao为一个class //使用注解方式注入 不需要get、set方法 只需要加入注解 @Autowired private BookDao bookDao; @Resource(name="bookDao")//name的值是BookDao注解里面value的值 private BookDao bookDao2; public void add() { System.out.println("service......"); } /** * //测试service调用到是否成功 */ @Test public void testA() { //加载配置文件 创建对象 ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); //得到创建的对象 BookService bookService = (BookService) context.getBean("bookService"); //调用dao的add方法 bookService.bookDao.add(); bookService.bookDao2.add(); } }xml:
<?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:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 注解方式 实现bean的创建 和属性注入 -->
<!-- 开启注解扫描 扫描包里面的类、方法、属性是否有注解 要扫描多个包 可用逗号隔开 这里扫描了三个包 -->
<context:component-scan base-package="per.spring.beans,per.spring.service,per.spring.dao"></context:component-scan>
<!-- 装配bean -->
<bean id="bookService" class="per.spring.service.BookService"></bean>
</beans>运行结果:
配置文件与注解方式混合使用: 用配置文件方式创建bean 用注解方式注入属性
相关文章推荐
- ######【spring属性注入(Ioc的DI)总结】:注解方式属性注入,属性名任意.=for理解:Aop注入代理对象时,注入被增强类对象时,属性名为proxy(自定义)。
- spring-03 之IOC 属性注入(配置文件方式 附代码)
- lesson one:spring ioc,属性注入方式,实现自己的beanfactoy
- Spring通过@Value注解注入属性的几种方式
- Spring通过@Value注解注入属性的几种方式
- ######Spring第5天ssh整合(注解版)难理解知识点:【自定义 方法加注解的方式】给dao注入sessionFactory属性+【搞清@Autowared和@Resource含义】
- Spring通过@Value注解注入属性的几种方式
- 04-Spring-使用ioc注解方式配置bean
- Spring 配置bean, 注解方式配置 xml方式要生产set方法方便属性注入,注解方式不需要,利用反射机制注入
- Spring依赖注入方式_属性setter方法注入_构造器注入_注解注入
- Spring IOC 依赖注入的两种方式:XML和注解
- spring详解:使用注解方式注入属性
- 详解Spring通过@Value注解注入属性的几种方式
- 【Spring】【IOC】【Spring容器注入Bean对象的四种方式】【Spring中注入bean对象的注解】
- Spring--IoC--域属性的注入-使用spring注解
- 从0开始写一个基于注解的轻量级分布式RPC框架(4)自定义Spring的IOC,自定义属性注入bean的过程
- Spring IOC 注入方式详解 附代码
- Spring使用注解的方式注入属性时的xml文件配置
- Spring实现控制反转(IOC)的三种方式(一)——属性注入
- spring学习(五)—通过注解方式创建对象和注入属性