1.集合[List,Set,Map]及配置文件[]的参数配置方式
applicationContext.xml
<!--集合及配置参数注入方式 -->
<bean id="example" class="cn.goktech.entity.Example">
<property name="interest">
<list>
<value>sing</value>
<value>dance</value>
<value>rap</value>
</list>
</property>
<property name="cities">
<set>
<value>北京</value>
<value>成都</value>
<value>上海</value>
</set>
</property>
<property name="like">
<map>
<entry key="Chinese" value="语文" />
<entry key="Math" value="数学" />
<entry key="English" value="英语"/>
</map>
</property>
<property name="info">
<props>
<prop key="username">Alice</prop>
<prop key="password">123456</prop>
<prop key="driver">com.jdbc.mysql.driver</prop>
</props>
</property>
</bean>
Test类,输出set集合中的值
public class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
Example e = context.getBean(Example.class);
System.out.println(e.getCities());
}
}
2.util标签的使用,util引用本地配置文件的方式
<property name="cities" ref="diySet">
<!--依赖注入,把下面id为diySet的Set注入,没有先后顺序,加载完后执行 -->
<!-- <set>-->
<!-- <value>北京</value>-->
<!-- <value>成都</value>-->
<!-- <value>上海</value>-->
<!-- </set>-->
</property>
<!--util 定义可重复使用的集合 -->
<util:set id="diySet"> <!--定义一个全局使用的集合,可以给定id和作用域 -->
<value>兰州</value>
<value>郑州</value>
<value>广州</value>
</util:set>
<!-- spring-expression jar包中有util的jar包-->
运行结果:
3.Spring EL表达式
jdbc.properties配置文件
url=jdbc:mysql://localhost:3306/test
username=root
password=123456
driver=com.mysql.jdbc.Driver
applicationContext.xml
<!-- 引用配置文件,localhost制定本地配置文件路径 -->
<util:properties id="jdbc" location="jdbc.properties" />
<!-- Spring EL表达式 #{id.属性} -->
<bean id="myConnection" class="cn.goktech.entity.MyConnection">
<property name="url" value="#{jdbc.url}"/>
<property name="driver" value="#{jdbc.driver}" />
<property name="username" value="#{example.interest[2]}" />
<property name="password" value="#{example.like.Chinese}" />
</bean>
Test类
public class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
Example e = context.getBean(Example.class);
System.out.println(e.getCities());
System.out.println(e.getInfo().get("driver"));
System.out.println("----------------");
MyConnection connection = context.getBean(MyConnection.class);
System.out.println(connection.getUrl());
System.out.println(connection.getUsername());
System.out.println(connection.getPassword());
}
}
输出结果:
4.自动装配autowired属性
Spring能自动装配Bean与Bean之间的依赖关系,即无须使用ref属性显式地指定依赖Bean,而是由Spring容器检查XML配置文件内容。
注: 自动装配可以减少配置文件的工作量,但是降低了依赖关系的透明性和确定性。
applicationContext.xml
<!-- 自动装配:default 不使用自动装配,byName 寻找跟属性名一致的bean的id -->
<bean id="a" class="cn.goktech.entity.A" />
<bean id="b" class="cn.goktech.entity.B" autowire="byName" />
byName的方式:将id修改了,就找不到了
Test类:
B b = context.getBean(B.class);
b.getA().af();
public class A {
public A(){
System.out.println("A类的构造执行");
}
public void af(){
System.out.println("A类方法调用成功");
}
}
public class B {
private A a;
public B(){
System.out.println("B类的构造执行");
}
public B(A a){
this.a = a;
}
public A getA() {
return a;
}
public void setA(A a) {
this.a = a;
}
}
运行结果:
(1)no:默认值,不使用自动装配,bean依赖关系完全由ref元素定义,在较大的部署环境中不鼓励改变这个配置,这样能使bean与bean之间依赖关系更清晰。
(2) byName: Spring容器以属性名作为id来查找对应的bean,调用set方法来完成注入
<bean id="a" class="cn.goktech.entity.A" />
<bean id="b" class="cn.goktech.entity.B" autowire="byName" /> <!--在B类中注入A类 -->
注: Spring仅仅会根据名字去查找,即使类型不匹配也会发生注入,只是在注入的时候抛出类型转换异常。如果找不到bean 则注入 null。
(3)byType :Spring容器查找与 class属性类型一致的bean,然后调用set方法来注入
有多个同名类时不适用。
注: 如果在xml配置文件中找到多个同类型bean,则抛出异常。
(4) constructor: 容器查找与class属性类型一致的bean,然后调用构造器里完成
5.基于注解的自动扫描
(1)导入aop及aspects的jar包
(2)添加扫描配置<context:component-scan base-package="">
(3)注解[@Componet,@Value,@Autowire,@Resource]
<!--基于注解的自动扫描,指定包,将扫描该包及该包的子包中的所有类 -->
<context:component-scan base-package="cn.goktech.anno"/>
在Student类前面加上@Component,在Teacher类前面也加上@Component
@Component
public class Student {
@Value("1001")
private int id;
@Value("Alice")
private String name;
//注入对象,setter注入推荐@Resource,构造器注入推荐@Autowire
@Autowired
private Teacher teacher;
@Component
public class Teacher {
@Value("001")
private int id;
@Value("#{jdbc.teacher}") //在jdbc中配置teacher=韩老师,EL表达式等价于 @Value("韩老师")
private String name;
注解和配置可以相互调用。
TestAnno
public class TestAnno {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
Student s = context.getBean(Student.class);
System.out.println(s.getName()+"——"+s.getTeacher().getName());
}
}
输出结果:
一旦扫描到就相当于配置了bean。
注入对象,setter注入推荐@Resource,构造器注入推荐@Autowire
@Resource只能用于setter注入,而@Autowire既能用于setter注入,也能用于构造器注入