注解的方式确实比手动写xml文件注入要方便快捷很多,省去了很多不必要的时间去写xml文件
按以往要注入bean的时候,需要去配置一个xml,当然也可以直接扫描包体,用xml注入bean有以下方法:
1 <?xml version="1.0" encoding="UTF-8"?>
2 <beans xmlns="http://www.springframework.org/schema/beans"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
4 xmlns:context="http://www.springframework.org/schema/context"
5 xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop"
6 xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
7 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
8 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
9 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
10 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
11
12 <!-- 这个就是springmvc里面的包扫描,只要是家了@Controller,@Service之类的,都会自动扫描并加入注册 -->
13 <context:component-scan base-package="com.lx.springmvcdemo" use-default-filters="false"></context:component-scan>
14
15
16 <!-- 这个就是spring里面的bean注册,scope是告诉spring是用什么模式,是单例还是工厂或者request或者session等 -->
17 <bean id="person" class="com.lx.springmvcdemo.bean.Color" scope="prototype">
18 <property name="yellow" value="yellow"></property>
19 <property name="red" value="red"></property>
20 </bean>
21
22 <bean ></bean>
23
24 <!-- 开启基于配置的的AOP -->
25 <aop:aspectj-autoproxy ></aop:aspectj-autoproxy>
26 </beans>
可以看到,如果是手动注入bean的话,需要配置很多东西,比如采用的模式,包的路径,id,默认属性值等等,但用注解,可以省略很多,而且更加灵活
首先我们先建立一个普通的类:
package com.lx.springmvcdemo.bean;
public class Color {
private String yellow;
public Color(String yellow, String red) {
super();
this.yellow = yellow;
this.red = red;
}
public Color() {
super();
// TODO Auto-generated constructor stub
}
@Override
public String toString() {
return "Color [yellow=" + yellow + ", red=" + red + "]";
}
public String getYellow() {
return yellow;
}
public void setYellow(String yellow) {
this.yellow = yellow;
}
public String getRed() {
return red;
}
public void setRed(String red) {
this.red = red;
}
private String red;
}
然后做一个配置类,我们起个名字叫MainConfig
package com.lx.springmvcdemo.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import com.lx.springmvcdemo.bean.*;
import com.lx.springmvcdemo.condition.LinuxCondition;
import com.lx.springmvcdemo.condition.WindowCondition;
//类中组建统一设置,这个conditional里面的是一个数组,只有满足这个条件,这个类里面的bean才会生效
//@Conditional({WindowCondition.class})
@Configuration
//快速导入组件的注解,import,可以使用单个,也可以使用数组,这样就会自动导入这个组件
//{Color.class,Red.class,MyImportSelecter.class,MyImportBeanDefinitionRegistart.class}
@Import({Blue.class})
public class MainConfigII {
/*Scope,设置组件作用域
* * @see ConfigurableBeanFactory#SCOPE_PROTOTYPE
* @see ConfigurableBeanFactory#SCOPE_SINGLETON
* @see org.springframework.web.context.WebApplicationContext#SCOPE_REQUEST
* @see org.springframework.web.context.WebApplicationContext#SCOPE_SESSION
* prototype:多实例,ioc启动的时候,并不会去添加这个person对象,而是在每次调用的时候new一次
* singleton:单实例,也是默认值,会一直使用同一个
* request:同一次请求创建一个实例
* session:同一个session创建一个实例
*
*
* @lazy
* 懒加载:
* 只针对单实例bean,默认容器启动的时候创建bean
* 而懒加载则是,容器启动的时候不创建对象,而是第一次使用时候
* 创建对象并初始化
*
* */
//@Scope("singleton")//这个确定是单例还是多实例
// @Lazy ///这个就是懒加载
// @Bean("person")
// public Person person() {
// System.out.println("启动,添加person类");
// return new Person("aj",33);
// }
/***
* Conditional:按照条件注册bean
* 这个conditional也可以注册到类上面
* 意思也是一样,就是满足conditional这个注解里面的条件,这个类下面的所以bean才会生效
* @return
*/
/***
* 给容器中注册组件的注解
* 1),@Bean():就像下面所演示的一样
* 2),@import():可以快速导入不需要操作很复杂的组件,可以直接用数组批量导入
* 其中,ID是默认的全类名
* ImportSelector:自定义返回bean,数组,
* importBeanDefinitionRegistart:手动注册bean到容器里面
*
* 3),直接扫描某一个包下面的全部bean
* 4),FactoryBean,这个是spring提供的工厂bean
* 1),默认获取调用的是bean调用getObject()的对象
* 2),要获取工厂本身的话,可以用&ColorFactoryBean来获取,即&加上bean的ID
* @return
*/
@Bean
public Color Color() {
return new Color();
}
}
以上代码里面都有很详细的注释
然后可以直接建立一个测试类
@Test
public void test02() {
// ApplicationContext applicationContext=new ClassPathXmlApplicationContext("bean.xml");
//通过AnnotationConfigApplicationContext获取配置文件
//然后通过bean的ID 获取bean
ApplicationContext applicationContext=new AnnotationConfigApplicationContext(MainConfigII.class);
Color c= (Color) applicationContext.getBean("Color");
System.out.println(c.toString());
// Person person2=(Person) applicationContext.getBean("person");
//System.out.println(person==person2);
}
以上。