Java Spring-注解进行属性注入
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.xsdhttp://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.");}@Overridepublic 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.");}@Overridepublic 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;@Overridepublic 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.xsdhttp://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;@Testpublic 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.xsdhttp://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/>,就可以使属性注入的注解生效。
转载于:https://www.cnblogs.com/TIMHY/p/7795560.html
Java Spring-注解进行属性注入相关推荐
- java spring框架 注解_史上最全的java spring注解
史上最全的java spring注解,没有之一 注解是个好东西,但好东西我们也是看见过,整理过,理解过,用过才知道好.不求我们每个都记住,但求保有印象,在需要的时候能提取出来再查找相关资料,平时工作就 ...
- @async注解_史上最全的java spring注解
史上最全的java spring注解,没有之一 注解是个好东西,但好东西我们也是看见过,整理过,理解过,用过才知道好.不求我们每个都记住,但求保有印象,在需要的时候能提取出来再查找相关资料,平时工作就 ...
- 这篇文章,我们来谈一谈Spring中的属性注入
本系列文章: 读源码,我们可以从第一行读起 你知道Spring是怎么解析配置类的吗? 配置类为什么要添加@Configuration注解? 谈谈Spring中的对象跟Bean,你知道Spring怎么创 ...
- springMVC通过spring.xml对属性注入bean值(工厂模式)
springMVC通过spring.xml对属性注入bean值,该bean是一个map容器: <bean id="configXMLCreatorFactory" class ...
- Spring注解开发-属性依赖注入
1.简单的属性注入.无需增加get/set方法 注意:@Value @Autowired它们可以修饰属性,也可以修饰setter方法,如果写在属性上,就不需要提供setter方法. 2.复杂的属性注入 ...
- Spring注解开发-属性依赖注入指定名称的bean
1.@Autowired它默认是根据类型进行注入.多个IUserDao会报错. 如果与@Qualifier一起使用,就可以根据名称来进行注入. 2.也可以使用下面的方式来根据名称进行属性注入:
- java spring注解维护,从一次工程启动失败谈谈 spring 注解
原标题:从一次工程启动失败谈谈 spring 注解 檀宝权 Java 后端开发工程师,负责度假 App 后端和广告后端开发维护工作,熟悉 Tomcat,Spring,Mybatis,会点 Python ...
- Spring学习笔记(二)——Spring相关配置属性注入Junit整合
一.Spring的相关配置 1.1 Bean元素 class属性:被管理对象的完整类名 name属性:给Bean起个名字,能重复,能使用特殊字符.后来属性 id属性:给Bean起个名字,不能重复,不能 ...
- Spring Bean的属性注入
在spring中bean的属性注入有两种 构造器注入 <bean id="car" class="nwtxxb.di.Car"><constr ...
最新文章
- 【大数据技术干货】阿里云伏羲(fuxi)调度器FuxiMaster功能简介(一) 多租户(QuotaGroup)管理...
- python统计单词频率、存放在字典中_Python3实现统计单词表中每个字母出现频率的方法示例...
- 牛客网 【每日一题】6月11日题目精讲 背包
- 金蝶携手工商银行完成首单数字人民币费用报销业务
- Delphi 7连接MySql 5 5 15
- 运行python的两种方式磁盘式_python计算机基础-Day1
- linux安装oracle
- Web---演示Servlet的相关类、下载技术、线程问题、自定义404页面
- 使用Java 8 Stream像操作SQL一样处理数据(上) 1
- robocode_Robocode大师的提示,技巧和建议的集合
- python坐标轴刻度设置为一个函数_Python坐标轴操作及设置代码实例
- 最近完成的APS生产排程工具,以甘特图展示排程结果
- Ubuntu20.04安装C++版Opencv4
- 一个Vue+Canvas的酷炫后台管理
- 网易16年春季实习生招聘的一道算法题
- webpack自定义loader
- 四天搞懂生成对抗网络(一)——通俗理解经典GAN
- PictureMerge
- AI把你打造成时尚界宠儿 1
- SpringBoot+MongoDB实现一个物流订单系统
热门文章
- 开源.Net邮件服务器-LumiSoft Mail Server简介
- 【Java从0到架构师】Maven - 依赖冲突、分模块构建项目
- 【jQuery笔记Part1】09-jQuery操作css-尺寸
- bug君你好啊之访问servlet时出现此程序可以连接到 Web 服务器,但是因为地址问题无法找到该网页。
- Linux命令之sed使用入门概述
- 设计模式14_组合结构
- pdns backend mysql_安装PowerDNS(与MySQL后端)和Poweradmin在Debian蚀刻
- 操作系统 第五章 IO管理
- Python爬虫之编辑cookie实例:必胜客餐厅
- python——pandas库之Series数据结构基础