SSH与SSM学习之Spring09——Spring使用注解配置
来源:互联网 发布:斗鱼诸葛网络 编辑:程序博客网 时间:2024/06/06 00:50
- SSH与SSM学习之Spring09Spring使用注解配置
- 一步骤
- 二将对象注册到容器
- 三修改对象的作用范围
- 四值类型注入
- 五引用类型注入
- 六初始化和销毁方法
- 七测试小例子
- 1 Car 汽车类
- 2 User 用户类
- 3 配置文件
- 4 测试代码
- 5 运行结果
- 八源码下载
SSH与SSM学习之Spring09——Spring使用注解配置
一、步骤
1.除了之前导入的包,还需要导入 spring-aop 包
2.开启使用注解代理配置文件
<?xml version="1.0" encoding="UTF-8" ?><beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd "><!-- 指定扫描com.qwm.spring2.bean报下的所有类中的注解. 注意:扫描包时.会扫描指定包下的所有子孙包--><context:component-scan base-package="com.qwm.spring2.bean"></context:component-scan></beans>
3.在类中使用注解完成配置
二、将对象注册到容器
把对象配置到容器中,可以下面的四个注解
@Component("user") //下面三个和这个其实是一样的,只是下面三个起到标识的作用@Service("user") // service层@Controller("user") // web层@Repository("user")// dao层
例如
//@Component("user") //下面三个和这个其实是一样的,只是下面三个起到标识的作用//@Service("user") // service层//@Controller("user") // web层@Repository("user")// dao层public class User { .....}
三、修改对象的作用范围
可以使用的注解是
@Scope
例如
//指定对象的作用范围@Scope(scopeName="singleton")public class User { ....}
或者
//指定对象的作用范围@Scope(scopeName="prototype")public class User { ....}
四、值类型注入
可以使用的注解是
@Value(...)
可以写在属性中,也可以写在set方法中
例如
//通过反射的Field赋值,破坏了封装性@Value("wiming")private String name;
或者
//通过set方法赋值,推荐使用.@Value("wiming")public void setName(String name){ this.name = name;}
五、引用类型注入
可以使用的注解如下
@Autowired //自动装配//问题:如果匹配多个类型一致的对象.将无法选择具体注入哪一个对象.@Qualifier(name)//使用@Qualifier注解告诉spring容器自动装配哪个名称的对象@Resource(name)//手动注入,指定注入哪个名称的对象
使用 @Autowired 如下
@Autowired //自动装配//问题:如果匹配多个类型一致的对象.将无法选择具体注入哪一个对象.private Car car;
使用 @Qualifier(name) 如下
@Qualifier("car2")//使用@Qualifier注解告诉spring容器自动装配哪个名称的对象 private Car car;
使用 @Resource(name) 如下
@Resource(name="car")//手动注入,指定注入哪个名称的对象 private Car car;
六、初始化和销毁方法
初始化方法可以使用 @PostConstruct 注解
例如
@PostConstruct //对象创建后调用public void init(){ System.out.println("--初始化方法--");}
销毁方法可以使用 @PreDestroy 注解
例如
@PreDestroy //对象销毁之前调用public void destory(){ System.out.println("--销毁方法--");}
七、测试小例子
7.1 Car 汽车类
package com.qwm.spring2.bean;import org.springframework.beans.factory.annotation.Value;import org.springframework.stereotype.Component;/** * @author:qiwenming * @date:2017/10/12 0012 23:34 * @description: */@Component("car")public class Car { @Value("玛莎拉蒂") private String name; @Value("绿色") private String color; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } @Override public String toString() { return "Car{" + "name='" + name + '\'' + ", color='" + color + '\'' + '}'; }}
7.2 User 用户类
package com.qwm.spring2.bean;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.beans.factory.annotation.Value;import org.springframework.context.annotation.Scope;import org.springframework.stereotype.Component;import org.springframework.stereotype.Repository;import javax.annotation.PostConstruct;import javax.annotation.PreDestroy;import javax.annotation.Resource;/** * @author:qiwenming * @date:2017/10/12 0012 23:34 * @description: *///@Component("user") //下面三个和这个其实是一样的,只是下面三个起到标识的作用//@Service("user") // service层//@Controller("user") // web层@Repository("user")// dao层//指定对象的作用范围@Scope(scopeName="singleton")public class User { @Value("wiming") private String name; @Value("18") private int age; //@Autowired //自动装配 //问题:如果匹配多个类型一致的对象.将无法选择具体注入哪一个对象. //@Qualifier("car2")//使用@Qualifier注解告诉spring容器自动装配哪个名称的对象 @Resource(name="car")//手动注入,指定注入哪个名称的对象 private Car car; @PostConstruct //对象创建后调用 public void init(){ System.out.println("--初始化方法--"); } @PreDestroy //对象销毁之前调用 public void destory(){ System.out.println("--销毁方法--"); } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Car getCar() { return car; } public void setCar(Car car) { this.car = car; } @Override public String toString() { return "User{" + "name='" + name + '\'' + ", age=" + age + ", car=" + car + '}'; }}
7.3 配置文件
<?xml version="1.0" encoding="UTF-8" ?><beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd "><!-- 指定扫描com.qwm.spring2.bean报下的所有类中的注解. 注意:扫描包时.会扫描指定包下的所有子孙包--><context:component-scan base-package="com.qwm.spring2.bean"></context:component-scan></beans>
7.4 测试代码
package com.qwm.spring2.a_annotation;import com.qwm.spring2.bean.User;import org.junit.Test;import org.springframework.context.support.ClassPathXmlApplicationContext;/** * @author:qiwenming * @date:2017/10/12 0012 23:39 * @description:注解 测试 */public class Demo { @Test public void test1(){ ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("com/qwm/spring2/applicationContext.xml"); User user = (User) ac.getBean("user"); System.out.println(user); ac.close(); }}
7.5 运行结果
--初始化方法--User{name='wiming', age=18, car=Car{name='玛莎拉蒂', color='绿色'}}--销毁方法--
八、源码下载
https://github.com/wimingxxx/spring01/tree/master/src/com/qwm/spring2
阅读全文
0 0
- SSH与SSM学习之Spring09——Spring使用注解配置
- SSH与SSM学习之Spring17——Spring中AOP之使用注解配置
- SSH与SSM学习之Spring23——Spring事务之注解配置方式管理事务
- SSH与SSM学习之Spring16——Spring中AOP之使用配置文件配置
- SSH与SSM学习之SSH整合02——单独配置Spring容器
- SSH与SSM学习之Spring22——Spring事务之xml配置方式管理事务
- SSH与SSM学习之SSH整合04——Struts2与Spring整合
- SSH与SSM学习之SSH整合06——Hibernate与Spring整合
- SSH与SSM学习之SSH整合07——Spring整合c3p0连接池
- SSH与SSM学习之SSH整合08——Spring整合Hibernate环境操作数据库
- SSH与SSM学习之SSH整合09——Spring的aop事务
- SSH与SSM学习之Spring20——Spring事务之Spring相关的基础
- SSH与SSM学习之SSH整合03——单独配置Struts2
- SSH与SSM学习之SSH整合05——单独配置Hibernate
- SSH与SSM学习之Spring10——Spring与junit整合测试
- SSH与SSM学习之Spring04——Spring属性注入之set方法注入
- SSH与SSM学习之Spring05——Spring属性注入之构造函数注入
- SSH与SSM学习之Spring06——Spring属性注入之p名称空间注入
- 10.22刷题记录
- 【C++】String拷贝(包含深拷贝浅拷贝)以及拷贝构造函数中几种调用的情况
- 从简入难makefile文件编写,Linux C++编程,简单vi命令
- 51Nod 独木舟
- javascript之(六)正则表达式
- SSH与SSM学习之Spring09——Spring使用注解配置
- 【分享】请回答1988(一)
- Android神器Xposed框架
- 输入一个日期判断是这一年的第几天?
- linux基本操作和Shell常用通配符
- 如何阅读计算机科学类的书
- MyEclipse2014a 最简单最有效的汉化方法---eclipse和其他版本通用
- Java基础总结
- 影片推荐:《机械姬》