切记:不骄不躁 不浮不沉 学会思考
简介:上一篇文章介绍了 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 用注解方式注入属性