2017-11-06 21:19:43

一、Spring的注解装配Bean
Spring2.5 引入使用注解去定义Bean

  • @Component 描述Spring框架中Bean

Spring的框架中提供了与@Component注解等效的三个注解

  • @Repository 用于对DAO实现类进行标注(dao层)
  • @Service 用于对Service实现类进行标注(service层)
  • @Controller 用于对Controller实现类进行标注(web层)
//因为只有一个属性value,所以可以直接写。一般需要value="..."
@Component("user")
public class User {
public void sayHello(){
System.out.println("Hello World.");
}
}

配置文件:

<?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.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- bean definitions here --> <context:component-scan base-package="spring5"/> </beans>

二、注解进行属性注入

普通属性:@Value(value="..."),这时候可以不写setter方法

对象属性:@Resource(name = "....")

或者采用   @Autowired

       @Qualifier(value = "plane")

//因为只有一个属性value,所以可以直接写。一般需要value="..."
@Component("user")
public class User {
@Value(value="Spring")
private String s; public void sayHello(){
System.out.println("Hello World.");
} @Override
public String toString() {
return "User{" +
"s='" + s + '\'' +
'}';
}
}

三、XML和注解的混合使用

两种方式结合:一般使用XML注册Bean,使用注解进行属性的注入

首先介绍一些其他的注解配置:

(1)配置 Bean 初始化方法和销毁方法 :
* init-method  和 destroy-method.

  • @PostConstruct  初始化
  • @PreDestroy 销毁
@PostConstruct
public void setup(){
System.out.println("初始化...");
}
@PreDestroy
public void teardown(){
System.out.println("销毁...");
}

(2) 配置 Bean 的作用范围 :@Scope

@Component("user")
@Scope(value="prototype")
public class User {
@Value(value="Spring")
private String s; public void sayHello(){
System.out.println("Hello World.");
} @Override
public String toString() {
return "User{" +
"s='" + s + '\'' +
'}';
}
}

(3)使用Java类来进行配置信息,在XML中扫描一下即可

@Configuration
public class BeanConfig {
@Bean(name = "car")
public Car showCar() {
Car car = new Car();
car.setName("长安");
car.setPrice(40000d);
return car;
} @Bean(name = "product")
public Product initProduct() {
Product product = new Product();
product.setName("空调");
product.setPrice(3000d);
return product;
}
}

混合使用范例:

public class Person {
@Autowired
@Qualifier("car")
private Car car;
@Autowired
@Qualifier(value = "plane")
private Plane plane; @Override
public String toString() {
return "Person{" +
"car=" + car +
", plane=" + plane +
'}';
}
}

配置文件:

<?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.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- bean definitions here --> <!--使属性注入注解生效-->
<context:annotation-config/> <bean id="car" class="spring6.Car"/>
<bean id="plane" class="spring6.Plane"/> <bean id="person" class="spring6.Person">
</bean> </beans>

四、集成Junit测试

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring-config.xml")
public class spring6 { @Autowired
@Qualifier("person")
private Person p; @Test
public void demo(){
System.out.println(p);
}
}

配置文件:

<?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.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- bean definitions here --> <!--使属性注入注解生效-->
<context:annotation-config/> <bean id="car" class="spring6.Car"/>
<bean id="plane" class="spring6.Plane"/> <bean id="person" class="spring6.Person">
</bean> </beans>

如果是注解进行的类注册,那么需要使用<context:component-scan base-package="spring5"/>来扫包,

不过一般bean的注册还是在XML文件中声明的,所以在注解方式中用的比较多的就是属性的注入操作了,这种操作只需要加上<context:annotation-config/>,就可以使属性注入的注解生效。

Java Spring-注解进行属性注入的更多相关文章

  1. 这篇文章,我们来谈一谈Spring中的属性注入

    本系列文章: 读源码,我们可以从第一行读起 你知道Spring是怎么解析配置类的吗? 配置类为什么要添加@Configuration注解? 谈谈Spring中的对象跟Bean,你知道Spring怎么创 ...

  2. Spring框架的属性注入

    1. 对于类成员变量,常用的注入方式有两种 * 构造函数注入(没有空的构造方法注入) * 属性setter方法注入(有空的构造方法注入) 2. 在Spring框架中提供了前两种的属性注入的方式 1. ...

  3. Spring学习笔记(二)——Spring相关配置&属性注入&Junit整合

    一.Spring的相关配置 1.1 Bean元素 class属性:被管理对象的完整类名 name属性:给Bean起个名字,能重复,能使用特殊字符.后来属性 id属性:给Bean起个名字,不能重复,不能 ...

  4. Spring笔记②--各种属性注入

    Ioc 反转控制 反转资源获取的方向 分离接口与实现 采用工厂模式 采用反转控制   Di 依赖注入 依赖容器把资源注入   配置bean 通过全类名(反射) 配置形式:基于xml方式 Ioc容器的b ...

  5. Spring 注解Autowired自动注入bean异常解决

      错误: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'xx' is defined ...

  6. Spring - IoC(2): 属性注入 & 构造注入

    依赖注入是指程序运行过程中,如果需要另外的对象协作(访问它的属性或调用它的方法)时,无须在代码中创建被调用者,而是依赖于外部容器的注入. 属性注入(Setter Injection) 属性注入是指 I ...

  7. Spring中的属性注入注解

    @Inject使用 JSR330规范实现的 默认按照类型注入 如果需要按照名称注入,@Inject需要和@Name一起使用 @Resource JSR250规范实现的,需要导入不同的包 @Resour ...

  8. 【Java Web开发学习】Spring构造器和属性注入

    测试类 public class Construct { private String address; private long phone; public Construct(String nam ...

  9. 8、Spring+Struts2+MyBaits(Spring注解+jdbc属性文件+log4j属性文件)

    一.注解理论 使用注解来构造IoC容器 用注解来向Spring容器注册Bean.需要在applicationContext.xml中注册<context:component-scan base- ...

随机推荐

  1. python的tqdm模块

    Tqdm 是一个快速,可扩展的Python进度条,可以在 Python 长循环中添加一个进度提示信息,用户只需要封装任意的迭代器 tqdm(iterator). 根据要求安装依赖即可. 可以很方便的在 ...

  2. 向Docx4j生成的word文档中添加布局--第二部分

    原文标题:Adding layout to your Docx4j-generated word documents, part 2 原文链接:http://blog.iprofs.nl/2012/1 ...

  3. find the safest road---hdu1596(最短路模板求最大概率)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1596 求给定的任意两点之间的最大安全概率,概率之间是相乘的关系,所以注意初始化即可 #include& ...

  4. vue.js(三)

    这里该记到vue的组件了,组件基础篇 1.vue组件的基本书写方式 Vue.component('button-counter', { data: function () { return { cou ...

  5. python面向对象(类和对象及三大特性)

    类和对象是什么 创建类 新式类 和 经典类 面向对象三大特性 继承 封装 多态   面向对象是一种编程方式,此编程方式的实现是基于对 类 和 对象 的使用 类 是一个模板,模板中包装了多个“函数”供使 ...

  6. 寻找最小(最大)的k个数

    题目描述:输入n个整数,输出其中最小的k个元素. 例如:输入1,2,3,4,5,6,7,8这8个数字,则最小的4个数字为1,2,3,4. 思路1:最容易想到的方法:先对这个序列从小到大排序,然后输出前 ...

  7. python笔记9-多线程Threading之阻塞(join)和守护线程(setDaemon)

    python笔记9-多线程Threading之阻塞(join)和守护线程(setDaemon) 前言 今天小编YOYO请xiaoming和xiaowang吃火锅,吃完火锅的时候会有以下三种场景: - ...

  8. Visual Studio 2017企业版 Enterprise 注册码 专业版Professional 激活码key

    Visual Studio 2017(VS2017) 企业版 Enterprise 注册码:NJVYC-BMHX2-G77MM-4XJMR-6Q8QFVisual Studio 2017(VS2017 ...

  9. 窄依赖与宽依赖&stage的划分依据

    RDD根据对父RDD的依赖关系,可分为窄依赖与宽依赖2种. 主要的区分之处在于父RDD的分区被多少个子RDD分区所依赖,如果一个就为窄依赖,多个则为宽依赖.更好的定义应该是: 窄依赖的定义是子RDD的 ...

  10. 史上最全的MonkeyRunner自动化测试从入门到精通(2)

    原文地址https://blog.csdn.net/liu_jing_hui/article/details/60955696 最基本脚本功能开始编写 (1)Monkeyrunner和Monkey的区 ...