在上一篇里已经模拟了通过 id 来配置获取 bean
依然是使用 Monster 类
<bean id="monster01" class="com.hspedu.spring.beans.Monster">
<property name="monsterId" value="1"/>
<property name="name" value="牛魔王"/>
<property name="skill" value="牛魔王拳"/>
</bean>
@Test
public void getMonsterByType() {
ApplicationContext ioc = new ClassPathXmlApplicationContext("beans.xml");
Monster monster = ioc.getBean(Monster.class);
System.out.println("monster=" + monster);
Monster monster2 = ioc.getBean(Monster.class);
System.out.println("monster == monster2 的值= " + (monster == monster2));
//是同一个对象
}
index 表示构造器的第几个参数,从 0 开始计数
<bean id="monster02" class="com.hspedu.spring.beans.Monster">
<constructor-arg value="2" index="0"/>
<constructor-arg value="蜘蛛精" index="1"/>
<constructor-arg value="吐口水" index="2"/>
</bean>
还可以通过 name/type 来指定参数方式
<bean id="monster03" class="com.hspedu.spring.beans.Monster">
<constructor-arg value="3" type="java.lang.Integer"/>
<constructor-arg value="白骨精" type="java.lang.String"/>
<constructor-arg value="白骨鞭" type="java.lang.String"/>
</bean>
<bean id="monster04" class="com.hspedu.spring.bean.Monster">
<constructor-arg value="200" name="monsterId"/>
<constructor-arg value="白骨精" name="name"/>
<constructor-arg value="吸人血" name="skill"/>
</bean>
@Test
public void getMonsterByConstructor() {
ApplicationContext ioc = new ClassPathXmlApplicationContext("beans.xml");
Object monster02 = ioc.getBean("monster02");
Object monster03 = ioc.getBean("monster03");
Object monster04 = ioc.getBean("monster03");
System.out.println("monster02= " + monster02);
System.out.println("monster03= " + monster03);
System.out.println("monster04= " + monster04);
}
增加命名空间配置
xmlns:p="http://www.springframework.org/schema/p"
通过 p 名称空间来配置 bean 对象,其实将光标放在 p 的地方,alt+enter 就可以添加 xmlns
<bean id="monster06" class="com.hspedu.spring.bean.Monster"
p:monsterId="500"
p:name="红孩儿"
p:skill="吐火"
/>
@Test
public void getMonsterByP() {
ApplicationContext ioc = new ClassPathXmlApplicationContext("beans.xml");
Monster monster04 = ioc.getBean("monster06", Monster.class);
System.out.println("monster06=" + monster06);
}
在 spring 的 ioc 容器, 可以通过 ref 来实现 bean 对象的相互引用
...\spring\dao\MemberDAOImpl.java
public class MemberDAOImpl {
public MemberDAOImpl() {
System.out.println("MemberDAOImpl 构造器...");
}
public void add() {
System.out.println("MemberDAOImpl add()方法");
}
}
...\spring\service\MemberServiceImpl.java
public class MemberServiceImpl {
private MemberDAOImpl memberDAO;
public MemberServiceImpl() {
System.out.println("MemberServiceImpl 构造器~");
}
public void add() {
System.out.println("MemberServiceImpl add()...");
memberDAO.add();
}
public void setMemberDAO(MemberDAOImpl memberDAO) {
this.memberDAO = memberDAO;
}
public MemberDAOImpl getMemberDAO() {
return memberDAO;
}
}
ref 表示 memberDAO 这个属性将引用/指向 id = memberDAOImpl 对象
就体现了 Spring 容器的依赖注入
注:在Spring容器中是作为一个整体运行的,创建引用是不分前后顺序
<bean id="memberServiceImpl" class="com.hspedu.spring.service.MemberServiceImpl">
<property name="memberDAO" ref="memberDAOImpl"/>
</bean>
<bean id="memberDAOImpl" class="com.hspedu.spring.dao.MemberDAOImpl"/>
@Test
public void setBeanByRef() {
ApplicationContext ioc = new ClassPathXmlApplicationContext("beans.xml");
MemberServiceImpl memberServiceImpl = ioc.getBean("memberServiceImpl",
MemberServiceImpl.class);
memberServiceImpl.add();
}
说明确实是引用关系
在 spring 的 ioc 容器, 可以直接配置内部 bean 对象
自己配置一个内部 bean
<bean id="memberServiceImpl02" class="com.hspedu.spring.service.MemberServiceImpl">
<property name="memberDAO">
<bean class="com.hspedu.spring.dao.MemberDAOImpl"/>
</property>
</bean>
@Test
public void setBeanByPro() {
ApplicationContext ioc = new ClassPathXmlApplicationContext("beans.xml");
MemberServiceImpl memberServiceImpl02 = ioc.getBean("memberServiceImpl02",
MemberServiceImpl.class);
memberServiceImpl02.add();
}
如何给 bean 对象的集合/数组类型属性赋值
...\spring\beans\Master.java
public class Master {
private String name;
private List<Monster> monsterList;
private Map<String, Monster> monsterMap;
private Set<Monster> monsterSet;
private String[] monsterName;
//这个 Properties 是 Hashtable 的子类 , 是 key-value 的形式
//这里 Properties key 和 value 都是 String
private Properties pros;
public Master() {}
public Master(String name) {this.name = name;}
public Set<Monster> getMonsterSet() {return monsterSet;}
public void setMonsterSet(Set<Monster> monsterSet) {
this.monsterSet = monsterSet;
}
public String[] getMonsterName() {return monsterName;}
public void setMonsterName(String[] monsterName) {
this.monsterName = monsterName;
}
public String getName() {return name;}
public void setName(String name) {
this.name = name;
}
public List<Monster> getMonsterList() {return monsterList;}
public void setMonsterList(List<Monster> monsterList) {
this.monsterList = monsterList;
}
public Map<String, Monster> getMonsterMap() {return monsterMap;}
public void setMonsterMap(Map<String, Monster> monsterMap) {
this.monsterMap = monsterMap;
}
public Properties getPros() {return pros;}
public void setPros(Properties pros) {
this.pros = pros;
}
}
给集合属性注入值
<bean id="master01" class="com.hspedu.spring.beans.Master">
<property name="name" value="太上老君"/>
<!-- 给 bean 对象的 list 集合赋值 -->
<property name="monsterList">
<list>
<ref bean="monster03"/>
<ref bean="monster02"/>
</list>
</property>
<!-- 给 bean 对象的 map 集合赋值 -->
<property name="monsterMap">
<map>
<entry>
<key>
<value>monsterKey01</value>
</key>
<ref bean="monster01"/>
</entry>
<entry>
<key>
<value>monsterKey02</value>
</key>
<ref bean="monster02"/>
</entry>
</map>
</property>
<!-- 给 bean 对象的 properties 集合赋值 -->
<property name="pros">
<props>
<prop key="k1">Java 工程师</prop>
<prop key="k2">前端工程师</prop>
<prop key="k3">大数据工程师</prop>
</props>
</property>
<!-- 给 bean 对象的数组属性注入值 -->
<property name="monsterName">
<array>
<value>银角大王</value>
<value>金角大王</value>
</array>
</property>
<!-- 给 bean 对象的 set 属性注入值 -->
<property name="monsterSet">
<set>
<ref bean="monster01"/>
<bean class="com.hspedu.spring.beans.Monster">
<property name="monsterId" value="10"/>
<property name="name" value="玉兔精"/>
<property name="skill" value="钻地洞"/>
</bean>
</set>
</property>
</bean>
@Test
public void setCollectionByPro() {
ApplicationContext ioc = new ClassPathXmlApplicationContext("beans.xml");
Master master01 = ioc.getBean("master01", Master.class);
//获取 list 集合
System.out.println("======list=======");
List<Monster> monster_list = master01.getMonsterList();
for (Monster monster : monster_list) {
System.out.println(monster);
}
//获取 map 集合
System.out.println("======map=======");
Map<String, Monster> monster_map = master01.getMonsterMap();
Set<Map.Entry<String, Monster>> entrySet = monster_map.entrySet();
for (Map.Entry<String, Monster> entry : entrySet) {
System.out.println(entry);
}
//获取 properties 集合
System.out.println("======properties=======");
Properties pros = master01.getPros();
String property1 = pros.getProperty("k1");
String property2 = pros.getProperty("k2");
String property3 = pros.getProperty("k3");
System.out.println(property1 + "\t" + property2 + "\t" + property3);
//获取数组
System.out.println("======数组=======");
String[] monsterName = master01.getMonsterName();
for (String s : monsterName) {
System.out.println("妖怪名= " + s);
}
//获取 set
System.out.println("======set=======");
Set<Monster> monsterSet = master01.getMonsterSet();
for (Monster monster : monsterSet) {
System.out.println(monster);
}
}
Properties 集合的特点:
spring 的 ioc 容器, 可以通过 util 名称空间创建 list 集合
public class BookStore {//书店
private List<String> bookList;
public BookStore() {}
public List<String> getBookList() {
return bookList;
}
public void setBookList(List<String> bookList) {
this.bookList = bookList;
}
}
增加配置
xmlns:util="http://www.springframework.org/schema/util"
对象配置:
<util:list id="myListBook">
<value>三国演义</value>
<value>西游记</value>
<value>红楼梦</value>
<value>水浒传</value>
</util:list>
<bean id="bookStore" class="com.hspedu.spring.beans.BookStore">
<property name="bookList" ref="myListBook"/>
</bean>
@Test
public void getListByUtil() {
ApplicationContext ioc = new ClassPathXmlApplicationContext("beans.xml");
BookStore bookStore = ioc.getBean("bookStore", BookStore.class);
List<String> bookList = bookStore.getBookList();
for (String s : bookList) {
System.out.println(s);
}
}
可以直接给对象属性的属性赋值, 即级联属性赋值
...\spring\beans\Dept.java
public class Dept {
private String name;
public Dept() {}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
...\spring\beans\Emp.java
public class Emp {
private String name;
private Dept dept;
public Emp() {}
public String getName() {return name;}
public void setName(String name) {
this.name = name;
}
public Dept getDept() {return dept;}
public void setDept(Dept dept) {
this.dept = dept;
}
}
<bean id="emp" class="com.hspedu.spring.beans.Emp">
<property name="name" value="jack"/>
<property name="dept" ref="dept"/>
<property name="dept.name" value="Java 开发部"/>
</bean>
<bean id="dept" class="com.hspedu.spring.beans.Dept"/>
@Test
public void setProByRelation() {
ApplicationContext ioc = new ClassPathXmlApplicationContext("beans.xml");
Emp emp = ioc.getBean("emp", Emp.class);
System.out.println(emp.getDept().getName());
}
...\spring\factory\MyStaticFactory.java
public class MyStaticFactory {
private static Map<String, Monster> monsterMap;
// 使用 static 代码块进行初始化
static {
monsterMap = new HashMap<String, Monster>();
monsterMap.put("monster01", new Monster(100, "黄袍怪", "一阳指"));
monsterMap.put("monster02", new Monster(200, "九头金雕", "如来神掌"));
}
public static Monster getMonster(String key) {
return monsterMap.get(key);
}
}
1. 通过静态工厂获取/配置bean
2. class 是静态工厂类的全路径
3. factory-method 表示是指定静态工厂类的哪个方法返回对象
4. constructor-arg value="monster02" value是指定要返回静态工厂的哪个对象
<bean id="my_monster01"
class="com.hspedu.spring.factory.MyStaticFactory"
factory-method="getMonster">
<constructor-arg value="monster02"/>
</bean>
@Test
public void getBeanByStaticFactory() {
ApplicationContext ioc = new ClassPathXmlApplicationContext("beans.xml");
Monster my_monster = ioc.getBean("my_monster01", Monster.class);
System.out.println(my_monster);
}
与静态工厂不同的地方在于,需要先有一个实例工厂才能获得对象
...\spring\factory\MyInstanceFactory.java
public class MyInstanceFactory {
private Map<String, Monster> monster_map;
//非静态代码块
{
monster_map = new HashMap<String, Monster>();
monster_map.put("monster_01", new Monster(100, "猴子精", "吃人"));
monster_map.put("monster_02", new Monster(200, "九头金雕", "如来神掌"));
}
public Monster getMonster(String key) {
return monster_map.get(key);
}
}
<bean id="myInstanceFactory" class="com.hspedu.spring.factory.MyInstanceFactory"/>
<bean id="my_monster2" factory-bean="myInstanceFactory"
factory-method="getMonster">
<constructor-arg value="monster_02"/>
</bean>
@Test
public void getBeanByInstanceFactory() {
ApplicationContext ioc = new ClassPathXmlApplicationContext("beans.xml");
Monster my_monster = ioc.getBean("my_monster2", Monster.class);
System.out.println(my_monster);
}
...\spring\factory\MyFactoryBean.java
public class MyFactoryBean implements FactoryBean<Monster> {
private String keyVal;
private Map<String, Monster> monster_map;
{ // 代码块完成初始化任务
monster_map = new HashMap<String, Monster>();
monster_map.put("monster_01", new Monster(100, "黄袍怪", "一阳指"));
monster_map.put("monster_02", new Monster(200, "九头金雕", "如来神掌"));
}
public void setKeyVal(String keyVal) {
this.keyVal = keyVal;
}
@Override
public Monster getObject() throws Exception {
// TODO Auto-generated method stub
return this.monster_map.get(keyVal);
}
@Override
public Class getObjectType() {
// TODO Auto-generated method stub
return Monster.class;
}
@Override
public boolean isSingleton() {
// TODO Auto-generated method stub
// 指定是否是返回单例
return true;
}
}
<bean id="myFactoryBean" class="com.hspedu.spring.factory.MyFactoryBean">
<property name="keyVal" value="monster_01"/>
</bean>
@Test
public void getBeanByFactoryBean() {
ApplicationContext ioc = new ClassPathXmlApplicationContext("beans.xml");
Monster monster = ioc.getBean("myFactoryBean", Monster.class);
System.out.println(monster);
}
在 spring 的 ioc 容器, 提供了一种继承的方式来实现 bean 配置信息的重用
如果要创建一个 配置信息是一样的对象的话,可以进行继承
<bean id="monster10" class="com.hspedu.spring.beans.Monster">
<property name="monsterId" value="10"/>
<property name="name" value="蜈蚣精"/>
<property name="skill" value="蜇人"/>
</bean>
<!-- parent="monster10" 就是继承使用了 monster10 的配置信息 -->
<bean id="monster11" class="com.hspedu.spring.beans.Monster" parent="monster10"/>
当我们把某个 bean 设置为 abstract="true" 这个 bean只能被继承,而不能实例化了,类似与java 的抽象类
<bean id="monster12" class="com.hspedu.spring.beans.Monster" abstract="true">
<property name="monsterId" value="12"/>
<property name="name" value="美女蛇"/>
<property name="skill" value="吃人"/>
</bean>
<!-- parent="monster12" 就是继承使用了 monster12 的配置信息 -->
<bean id="monster13" class="com.hspedu.spring.beans.Monster" parent="monster12"/>
@Test
public void getBeanByExtends() {
ApplicationContext ioc = new ClassPathXmlApplicationContext("beans.xml");
Monster monster1 = ioc.getBean("monster11", Monster.class);
System.out.println(monster1);
Monster monster2 = (Monster) ioc.getBean("monster13", Monster.class);
System.out.println(monster2);
}
在 spring 的 ioc 容器, 默认是按照配置的顺序创建 bean 对象
<bean id="student01" class="com.hspedu.bean.Student" />
<bean id="department01" class="com.hspedu.bean.Department" />
会先创建 student01 这个 bean 对象,然后创建 department01 这个 bean 对象
如果这样配置,增加了 depends-on="xxx"
<bean id="student01" class="com.hspedu.bean.Student"
depends-on="department01"/>
<bean id="department01" class="com.hspedu.bean.Department" />
会先创建 department01 对象,再创建 student01 对象
在默认是按照单例创建的,即配置一个 bean 对象后,ioc 容器只会创建一个 bean 实例
想要以多个实例形式创建某个 bean 对象的则可以通过配置scope="prototype" 来指定
...\spring\beans\Car.java
public class Car {
public Car() {
System.out.println("car 构造器");
}
}
<bean name="car" scope="prototype" class="com.hspedu.spring.beans.Car"/>
@Test
public void getBeanByPrototype() {
ApplicationContext ioc = new ClassPathXmlApplicationContext("beans.xml");
for (int i = 0; i < 3; i++) {
Car car = ioc.getBean("car", Car.class);
System.out.println(car);
}
}
?bean 对象创建是由 JVM 完成的,然后执行如下方法
...\spring\beans\House.java
public class House {
private String name;
public House() {
System.out.println("House() 构造器");
}
public String getName() {
return name;
}
public void setName(String name) {
System.out.println("House setName()...");
this.name = name;
}
public void init() {
System.out.println("House init()..");
}
public void destory() {
System.out.println("House destory()..");
}
}
配置 bean 的初始化方法和销毁方法?
<bean id="house" class="com.hspedu.spring.beans.House"
init-method="init"
destroy-method="destory">
<property name="name" value="北京豪宅"/>
</bean>
@Test
public void beanLife() {
ApplicationContext ioc = new ClassPathXmlApplicationContext("beans.xml");
House house = ioc.getBean("house", House.class);
System.out.println(house);
//关闭容器 ConfigurableApplicationContext 是有 close 方法的
((ConfigurableApplicationContext) ioc).close();
}
...\spring\beans\MyBeanPostProcessor.java
要实现?BeanPostProcessor 接口的两个方法作为一个前一个后
public class MyBeanPostProcessor implements BeanPostProcessor {
/**
* 在 bean 初始化之前完成某些任务
* @param bean: 就是 ioc 容器返回的 bean 对象,
* 如果被替换会修改,返回的 bean 对象也会被修改
* @param beanName: 就是 ioc 容器配置的 bean 的名称
* @return Object: 就是返回的 bean 对象
*/
public Object postProcessBeforeInitialization(Object bean, String beanName)
throws BeansException {
// TODO Auto-generated method stub
System.out.println("postProcessBeforeInitialization被 调 用"
+ beanName + "bean= " + bean.getClass());
return bean;
}
/**
* 在 bean 初始化之后完成某些任务
* @param bean: 就是 ioc 容器返回的 bean 对象, 如果被替换会修改,
* 返回的 bean 对象也会被修改
* @param beanName: 就是 ioc 容器配置的 bean 的名称
* @return Object: 就是返回的 bean 对象
*/
public Object postProcessAfterInitialization(Object bean, String beanName)
throws BeansException {
System.out.println("postProcessAfterInitialization 被调用 "
+ beanName + " bean= "+ bean.getClass());
return bean;
}
}
<bean id="house" class="com.hspedu.spring.beans.House"
init-method="init"
destroy-method="destory">
<property name="name" value="北京豪宅"/>
</bean>
?bean 后置处理器的配置,配置后这个后置处理器作用在该容器创建的 bean 对象中
<bean id="myBeanPostProcessor"
class="com.hspedu.spring.beans.MyBeanPostProcessor" />
</beans>
@Test
public void testBeanPostProcessor() {
ApplicationContext ioc = new ClassPathXmlApplicationContext("beans02.xml");
House house = ioc.getBean("house", House.class);
System.out.println(house);
//关闭容器
((ConfigurableApplicationContext) ioc).close();
}
就已经可以触发后置处理器了!!!
1. 怎么执行到这个方法?
使用 AOP(反射+动态代理+IO+容器+注解)
2、有什么用?
可以对 IOC 容器中所有的对象进行统一处理 ,比如 日志处理/权限的校验/安全的验证/事务管理
3、针对容器的所有对象吗?
是的 => 这也就是AOP切面编程特点
在 src 目录下创建 注以是 Unicode 编码
name=\u9EC4\u888D\u602A
id=10
skill=\u72EE\u5B50\u543C
1. 通过属性文件给 bean 注入值,
2. 需要导入: xmlns:context 名字空间,并指定属性文件路径 location="xxxx"
<context:property-placeholder location="classpath:my.properties"/>
<bean id="monster100" class="com.hspedu.spring.beans.Monster">
<property name="monsterId" value="${id}"/>
<property name="name" value="${name}"/>
<property name="skill" value="${skill}"/>
</bean>
@Test
public void setProByProFile() {
ApplicationContext ioc = new ClassPathXmlApplicationContext("beans.xml");
Monster monster100 = ioc.getBean("monster100", Monster.class);
System.out.println(monster100);
}
...\spring\dao\OrderDao.java
public class OrderDao {
public void saveOrder() {
System.out.println("保存 一个订单...");
}
}
...\spring\service\OrderService.java
public class OrderService {
private OrderDao orderDao;
public OrderDao getOrderDao() {
return orderDao;
}
public void setOrderDao(OrderDao orderDao) {
this.orderDao = orderDao;
}
}
...\spring\action\OrderAction.java
public class OrderAction {
private OrderService orderService;
public OrderService getOrderService() {
return orderService;
}
public void setOrderService(OrderService orderService) {
this.orderService = orderService;
}
}
autowire="byType" 表示根据类型进行自动组装
比如OrderService 对象有 private OrderDao orderDao,就会在容器中去找有没有 OrderDao类型对象;如果有,就会自动的装配? -->?这个容器中,不能有两个的OrderDao类型对象
<bean id="orderAction" autowire="byType"
class="com.hspedu.spring.action.OrderAction"/>
<bean id="orderService" autowire="byType"
class="com.hspedu.spring.service.OrderService"/>
<bean id="orderDao" class="com.hspedu.spring.dao.OrderDao"/>
autowire = "byName" 表示通过名字完成自动装配
OrderService 属性 private OrderDao orderDao,根据这个属性的setXxx()方法的 xxx 来找对象id,public void setOrderDao() 就会找id=orderDao对象来进行自动装配,如果没有就装配失败
<bean autowire="byName" class="com.hspedu.spring.service.OrderService"
id="orderService"/>
<bean autowire="byName" class="com.hspedu.spring.web.OrderAction"
id="orderAction"/>
<bean id="orderDao" class="com.hspedu.spring.dao.OrderDao"/>
@Test
public void setProByAutowire() {
ApplicationContext ioc = new ClassPathXmlApplicationContext("beans.xml");
OrderAction orderAction = ioc.getBean("orderAction", OrderAction.class);
orderAction.getOrderService().getOrderDao().saveOrder();
}
这个知识点作为了解即可,后面主要还是使用基于注解的方式
支持运行时查询并可以操作对象
...\spring\beans\SpELBean.java
public class SpELBean {
private String name;
private Monster monster;
private String monsterName;
private String crySound;
private String bookName;
private Double result;
public SpELBean() {}
public String getName() {return name;}
public void setName(String name) {this.name = name;}
public Monster getMonster() {return monster;}
public void setMonster(Monster monster) {this.monster = monster;}
public String getMonsterName() {return monsterName;}
public void setMonsterName(String monsterName) {this.monsterName = monsterName;}
public String getCrySound() {return crySound;}
public void setCrySound(String crySound) {this.crySound = crySound;}
public String getBookName() {return bookName;}
public void setBookName(String bookName) {this.bookName = bookName;}
public Double getResult() {return result;}
public void setResult(Double result) {this.result = result;}
public String cry(String sound) {return "发出 " + sound + "叫声...";}
public static String read(String bookName) {return "正在看 " + bookName;}
}
<bean id="spELBean" class="com.hspedu.spring.beans.SpELBean">
<!-- sp el 给字面量 -->
<property name="name" value="#{'hsp教育'}"/>
<!-- sp el 引用其它 bean -->
<property name="monster" value="#{monster01}"/>
<!-- sp el 引用其它 bean 的属性值 -->
<property name="monsterName" value="#{monster02.name}"/>
<!-- sp el 调用普通方法赋值 -->
<property name="crySound" value="#{spELBean.cry('喵喵的..')}"/>
<!-- sp el 调用静态方法 赋值 -->
<property name="bookName" value="#{T(com.hspedu.spring.beans.SpELBean).
read('天龙八部')}"/>
<!-- sp el 通过运算赋值 -->
<property name="result" value="#{89*1.2}"/>
</bean>
@Test
public void setProBySpel() {
ApplicationContext ioc = new ClassPathXmlApplicationContext("beans.xml");
SpELBean spELBean = ioc.getBean("spELBean", SpELBean.class);
System.out.println(spELBean.getName());
System.out.println(spELBean.getMonster());
System.out.println(spELBean.getMonsterName());
System.out.println(spELBean.getCrySound());
System.out.println(spELBean.getBookName());
System.out.println(spELBean.getResult());
}
不是重点,如果看到有人这样使用,能看懂即可
基于注解的方式配置 bean, 主要是项目开发中的组件,比如 Controller、Service、和 Dao
组件注解的形式有:
@Repository
public class UserDao {
}
@Service
public class UserService {
}
@Controller
public class UserAction {
}
@Component
public class MyComponent {
}
配置自动扫描的包,注意需要加入 context 名称空间
<context:component-scan base-package="com.hspedu.spring.component" />
@Test
public void getBeanByAnnotation() {
ApplicationContext ioc = new ClassPathXmlApplicationContext("beans.xml");
UserAction userAction = ioc.getBean(UserAction.class);
System.out.println(userAction);
UserDao userDao = ioc.getBean(UserDao.class);
System.out.println(userDao);
MyComponent myComponent = ioc.getBean(MyComponent.class);
System.out.println(myComponent);
UserService userService = ioc.getBean(UserService.class);
System.out.println(userService);
}
调试查看 ioc 容器
?Spring 的 IOC 容器不能检测一个使用了@Controller 注解的类到底是不是一个真正的控制器
?注解的名称是用于程序员自己识别当前标识的是什么组件。
其它的@Service @Repository 也是一样的道理 (也就是说 spring 的 IOC 容器只要检查到注解就会生成对象,但是这个注解的含义 spring 不会识别,注解是给程序员编程方便看的)
<context:component-scan base-package="com.hspedu.spring.component">
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Service"/>
</context>
目的:只扫描?Service??Controller
<context:component-scan base-package="com.hspedu.spring.component"
use-default-filters="false">
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Service"/>
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
默认情况:标记注解后,类名首字母小写作为 id 的值
也可以使用注解的 value 属性指定 id 值,并且 value 可以省略
@Controller(value="userAction01")
@Controller("userAction01")
链接:彻底弄懂@Controller 、@Service、@Component
?