ApplicationContext与BeanFactory区别
BeanFactory 采取延迟加载,第一次getBean时才会初始化Bean
ApplicationContext是对BeanFactory扩展,提供了更多功能(直接创建了对象)国际化处理,事件传递,Bean自动装配,各种不同应用层的Context实现
依赖注入概述
三种形式:构造函数注入,属性注入(常用),接口注入(并未实现)
构造函数注入:
public class C {
private D d;
public C(D d)
{
this.d = d;
}
public void test()
{
System.out.println(d);
}
}
public class D {
}
<!-- 构造函数注入 -->
<bean id="c" class="com.fcx.test.C">
<!-- 构造器注入定义变量 名字 ,ref也是名,构造器里面名 -->
<constructor-arg name="d" ref="d"></constructor-arg>
<!-- <property name="d" ref="d"></property> -->
</bean>
@Test
public void test4()
{
//启动String的容器
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//从容器获取A对象
C c = context.getBean(C.class);
c.test();
}
值注入:
public class ValueInject {
private String username;
private String password;
public void setUsername(String username) {
this.username = username;
}
public void setPassword(String password) {
this.password = password;
}
public void showInfo() {
System.out.println(username);
System.out.println(password);
}
}
<bean id="valueInjection" class="com.fcx.test.ValueInject">
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</bean>
@Test
public void test5()
{
//1.启动spring的容器
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//2.从容器获取A对象
ValueInject v = context.getBean(ValueInject.class);
v.showInfo();
}
集合注入:
public class ValueInject {
private String username;
private String password;
private List<String> list;
private Set<String> set;
private HashMap<String, String> map;
//p里都是名值对,还可以直接读取.propertis文件
private Properties p;
public void setList(List<String> list) {
this.list = list;
}
public void setSet(Set<String> set) {
this.set = set;
}
public void setMap(HashMap<String, String> map) {
this.map = map;
}
public void setP(Properties p) {
this.p = p;
}
public void setUsername(String username) {
this.username = username;
}
public void setPassword(String password) {
this.password = password;
}
//java8新特性
public void showInfo()
{
System.out.println(username);
System.out.println(password);
//遍历list
/*for(String str:list)
{
System.out.println(str);
}*/
list.forEach(str->{
System.out.println(str);
});
//遍历set
/*for(String str:set)
{
System.out.println(str);
}*/
set.forEach(str->{
System.out.println(str);
});
//遍历map
/*Set<Entry<String, String>> entries = map.entrySet();
for(Entry<String, String> entry: entries)
{
System.out.println(entry.getKey()+"\t"+entry.getValue());
}*/
map.forEach((k,v)->{
System.out.println(k+"\t"+v);
});
//遍历p
/*Set<Entry<Object, Object>> items = p.entrySet();
for(Entry<Object, Object> item: items)
{
System.out.println(item.getKey()+"\t"+item.getValue());
}*/
p.forEach((k,v)->{
System.out.println(k+"\t"+v);
});
}
}
<bean id="valueInjection" class="com.fcx.test.ValueInject">
<property name="username" value="root"></property>
<property name="password" value="root"></property>
<property name="list">
<list>
<value>aa</value>
<value>bb</value>
<value>cc</value>
</list>
</property>
<property name="set">
<list>
<value>ee</value>
<value>ff</value>
<value>gg</value>
</list>
</property>
<property name="map">
<map>
<entry key="driver" value="com.mysql.driver">
</entry>
<entry key="username" value="root">
</entry>
<entry key="password" value="root">
</entry>
</map>
</property>
<property name="p">
<props>
<prop key="driver">com.mysql.driver</prop>
<prop key="username">root</prop>
<prop key="password">root</prop>
</props>
</property>
</bean>
@Test
public void test5()
{
//1.启动spring的容器
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//2.从容器获取A对象
ValueInject v = context.getBean(ValueInject.class);
v.showInfo();
}