spring-context注入相关知识
1.第一种启动容器方式
<!–
告诉spring框架需要注入一个accountDao ref 引用的意思 必然存在容器
自动创建service 里面就自动将需要对象注入进来
–>
<property name="accountDao" ref="accountDao"/>
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
AccountService accountService1 = (AccountService) context.getBean("accountService");
System.out.println(accountService1);
AccountService accountService2 = (AccountService) context.getBean("accountService");
System.out.println(accountService2);
2.第二种容器启动方式
@Test
public void test2() {
// 加载跟目录下的配置文件
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
Resource res = resolver.getResource("beans.xml");
// 创建Bean工厂类 注意 XmlBeanFactory已经过期!
BeanFactory factory = new XmlBeanFactory(res);
AccountService accountService = factory.getBean("accountService", AccountService.class);
System.out.println(accountService);
}
3.为对象注入属性
<bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl"
p:aa="我是a" p:bb="我是bb"
>
@Test
public void test3() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
AccountService accountService = (AccountService) context.getBean("accountService");
accountService.transfer("小明","小红",1000);
}
4.注入简单属性
<property name="id" value="1"/>
<property name="name" value="我是一个属性"/>
/**
* 普通属性注入问题
*/
@Test
public void test4() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
AccountService accountService = (AccountService) context.getBean("accountService");
System.out.println("111");
}
5.注入集合属性
<property name="name" value="我是一个属性"/>
<property name="ss">
<array>
<value>小红</value>
<value>小明</value>
<value>小兰</value>
</array>
</property>
<property name="list">
<list>
<value>小红1</value>
<value>小明1</value>
<value>小兰1</value>
</list>
</property>
<property name="set">
<set>
<value>小红2</value>
<value>小明2</value>
<value>小兰2</value>
</set>
</property>
<property name="map">
<map>
<entry key="name" value="小阿明"/>
<entry key="age" value="18"/>
<entry key="hobbies" value="困觉"/>
</map>
</property>
/**
* 集合属性注入问题
*/
@Test
public void test5() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
AccountService accountService = (AccountService) context.getBean("accountService");
System.out.println("111");
}
6.构造函数注入
<constructor-arg name="desc" value="描述"></constructor-arg>
<constructor-arg name="xx" value="xxx"></constructor-arg>
/**
* 构造函数注入属性
*/
@Test
public void test6() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
AccountService accountService = (AccountService) context.getBean("accountService");
System.out.println("111");
}
7.通过p命名空间注入
<bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl"
p:aa="我是a" p:bb="我是bb"
>
/**
* 通过 p命名空间注入
*/
@Test
public void test7() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
AccountService accountService = (AccountService) context.getBean("accountService");
System.out.println("111");
}
8.静态工厂方式创建
<!--
静态工厂方法模式基本不用 基本不见8
-->
<bean id="myBean1" class="com.itheima.pojo.MyBeanFactory" factory-method="getMyBean" ></bean>
<bean id="instanceFactory" class="com.itheima.pojo.InstanceBeanFactory"></bean>
package com.itheima.pojo;
public class InstanceBeanFactory {
public MyBean getMyBean(){
MyBean myBean = new MyBean();
//伪代码 处理复杂过程
return myBean;
}
}
/**
* 静态工厂模式创建
*/
@Test
public void test8() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
MyBean myBean = (MyBean) context.getBean("myBean1");
System.out.println(myBean);
}
9.实例工厂模式
<!--
实例工厂创建 见的不多
-->
<bean id="myBean2" factory-bean="instanceFactory" factory-method="getMyBean"></bean>
/**
* 实例工厂模式
*/
@Test
public void test9() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
MyBean myBean = (MyBean) context.getBean("myBean2");
System.out.println(myBean);
}
10.factoryBean模式
<!--
factoryBean创建 见的多点
spring把实例工厂给标准化了
-->
<bean id="myBean3" class="com.itheima.pojo.MyBeanFactoryBean"></bean>
<!--
factoryBean一个实例
-->
<!--<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
</bean>-->
<!--<bean id="accountDao1" class="com.itheima.dao.impl.AccountDaoImpl"></bean>
<bean id="accountDao2" class="com.itheima.dao.impl.AccountDaoImpl"></bean>-->
package com.itheima.pojo;
import org.springframework.beans.factory.FactoryBean;
public class MyBeanFactoryBean implements FactoryBean<MyBean> {
@Override
public MyBean getObject() throws Exception {
MyBean myBean = new MyBean();
return myBean;
}
@Override
public Class<?> getObjectType() {
return MyBean.class;
}
}
/**
* factoryBean模式
*/
@Test
public void test10() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
MyBean myBean = (MyBean) context.getBean("myBean3");
System.out.println(myBean);
}
/**
* factoryBean模式
*/
@Test
public void test11() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
AccountDao accountDao1= (AccountDao) context.getBean("accountDao1");
System.out.println(accountDao1);
AccountDao accountDao2= (AccountDao) context.getBean("accountDao2");
System.out.println(accountDao2);
}
11.创建多例对象
<!--
演示多例的
-->
<!--<bean id="accountDao" class="com.itheima.dao.impl.AccountDaoImpl" scope="prototype"></bean>-->
<!--
/**
* spring 帮助创建多例对象
*/
@Test
public void test12() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
AccountDao accountDao1= (AccountDao) context.getBean("accountDao");
AccountDao accountDao2= (AccountDao) context.getBean("accountDao");
System.out.println(accountDao1);
System.out.println(accountDao2);
}
/**
* spring 帮助创建多例对象
*/
@Test
public void test13() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
AccountDao accountDao1= (AccountDao) context.getBean("accountDao");
context.close();
}
}