Spring基础知识(3)
来源:互联网 发布:wav分割软件 编辑:程序博客网 时间:2024/05/14 17:28
一、IoC容器装配Bean(注解方式)
1、使用注解方式进行Bean注册
xml 方式: <bean id="" class="">
spring2.5版本 提供一组注解,完成Bean注册
* @Component 描述Spring框架中Bean
第一步:
编写class,在类声明上添加@component
@Component("helloService")
// <bean id="helloService" class="...." />
public class HelloService {
}
第二步:
编写applicationContext.xml,--->通知Spring 注解类所在包
* 需要引入 context 名称空间
<context:component-scan base-package="cn.itcast.spring.a_beandefinition"></context:component-scan>
spring2.5 引入@Component 等效三个衍生注解
* @Repository 用于对DAO实现类进行标注 (持久层)
* @Service 用于对Service实现类进行标注 (业务层)
* @Controller 用于对Controller实现类进行标注 (表现层)
2、属性依赖注入
1) 简单属性的注入 通过 @Value注解完成
2) 复杂属性注入,通过@Autowired注解 完成Bean自动装配
@Autowired 默认按照类型进行注入
@Service("userService")public class UserService {//简单属性@Value("wade")private String name;//复杂对象@Autowiredprivate UserDao userDao;@Overridepublic String toString() {return "UserService [name=" + name + ", userDao=" + userDao + "]";}}
@Repository("userDao")public class UserDao {}
public class SpringTest {@Testpublic void demo1(){ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");UserService userService = (UserService) applicationContext.getBean("userService");System.out.println(userService);}}☆注意:@Value、 @Autowired注解都可以修饰 成员变量 或者 setter方法,如果修改成员变量,不需要提供setter方法
3) 使用@Resource注解 完成复杂对象Bean装配
@Resource和@Autowired注解功能相似
@Autowired
@Qualifer("userDAO")
private UserDAO userDAO ;
等价于
@Resource(name="userDAO")
private UserDAO userDAO ;
3、Bean其它属性设置
1) 指定Bean的初始化方法和销毁方法(注解) <bean init-method="" destroy-method="" />
@PostConstruct 作用 init-method
@PreDestroy 作用 destroy-method
@Component("lifeCycleBean")public class LifeCycleBean {@PostConstructpublic void setup(){System.out.println("初始化……");}@PreDestroypublic void teardown(){System.out.println("销毁……");}}
public class SpringTest {@Testpublic void demo1(){//ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");LifeCycleBean lifeCycleBean = (LifeCycleBean) applicationContext.getBean("lifeCycleBean");System.out.println(lifeCycleBean);//☆注意:如果想要销毁方法执行,必须销毁ApplicationContext,此时,使用ApplicationContext对象是不行的,要使用其子类对象:<span style="font-family: Arial, Helvetica, sans-serif;">ClassPathXmlApplicationContext</span>applicationContext.close();}
2) Bean的作用范围 <bean scope="" />
@Scope 注解 ,默认作用域 singleton 单例
@Component("scopeBean")//如果没有指定scope,默认是单例singleton@Scope("prototype")public class ScopeBean {}
@Testpublic void demo2(){ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");ScopeBean scopeBean = (ScopeBean) applicationContext.getBean("scopeBean");System.out.println(scopeBean);ScopeBean scopeBean2 = (ScopeBean) applicationContext.getBean("scopeBean");System.out.println(scopeBean2);}
添加@Scope("prototype")之前效果:
添加之后:
4、Spring3.0 提供 注册Bean的注解
@Configuration 指定POJO类为Spring提供Bean定义信息
@Bean 提供一个Bean定义信息
以JavaConfig技术为核心,JavaConfig技术:其思想就是用一个java类来作为配置文件。
public class Car {private String name;private double price;public String getName() {return name;}public void setName(String name) {this.name = name;}public double getPrice() {return price;}public void setPrice(double price) {this.price = price;}@Overridepublic String toString() {return "Car [name=" + name + ", price=" + price + "]";}}
public class Product {private String pname;private int pnum;public String getPname() {return pname;}public void setPname(String pname) {this.pname = pname;}public int getPnum() {return pnum;}public void setPnum(int pnum) {this.pnum = pnum;}@Overridepublic String toString() {return "Product [pname=" + pname + ", pnum=" + pnum + "]";}}
@Configurationpublic class BeanConfig {@Bean(name="car")public Car initCar(){Car car = new Car();car.setName("奥迪");car.setPrice(400000);return car;}@Bean(name="product")public Product showProduct(){Product product = new Product();product.setPname("电脑");product.setPnum(5);return product;}}
public class SpringTest {@Testpublic void demo(){ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");Car car = (Car) applicationContext.getBean("car");System.out.println(car);Product product = (Product) applicationContext.getBean("product");System.out.println(product);}}结果:
- Spring基础知识(3)
- spring基础知识3:aop
- 3 spring基础知识总结
- Spring基础知识(1)
- Spring基础知识(2)
- Spring基础知识(4)
- Spring基础知识(一)
- Spring基础知识(二)
- Spring基础知识(三)
- Spring基础知识(一)
- Spring基础知识(5)-AOP
- Spring基础知识(6)-AOP2
- Spring基础知识(7)-Aspectj
- Spring基础知识(8)-JdbcTemplate
- Spring基础知识(9)-事务管理
- Spring基础学习-(1)Spring基础知识
- spring基础知识
- Spring基础知识
- 使用Entity Framework CodeFirst模式创建新数据库
- 转载《CSDC长沙开发者大会感想》
- away3d MaterialBase
- C#中的序列化和反序列化是什么、有什么作用、使用方法详解
- C#的泛型约束-
- Spring基础知识(3)
- iOS实现录音功能
- 如何把eclipse的项目托管到github
- C#调用wcf服务
- ACM必备
- 学习Entity Framework 中的Code First
- 在自己应用中打开第三方应用
- 网站架构
- How to set up MultiComboBoxColumn?