1.Spring配置
(1)别名
<alias name="user" alias="newUser"/>
(2)Bean的配置
id是bean的标识符,如果没有配置id,那么就是默认标识符,如果即有id,又有name,那么name就是别名,name可以设置多个别名,用逗号或者空格隔开。
<bean id="user" name="u1 u2" class="demo1.User">
<property name="name" value="张三"/>
<property name="age" value="25"/>
</bean>
测试通过别名u1获取user
@Test
public void userTest() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("userbeans.xml");
//User bean = (User) context.getBean("user");
User bean = context.getBean("u1", User.class);
System.out.println(bean.getName() + "---" + bean.getAge());
}
结果
(3)import
配置文件中可以导入多个配置文件,主要用于团队开发
<import resource="beans.xml"/>
<import resource="userbeans.xml"/>
2.依赖注入
(1)配置环境
<1>实体类
Address
public class Address {
private String address;
public Address() {
}
public Address(String address) {
this.address = address;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Address{" +
"address='" + address + '\'' +
'}';
}
}
Student
public class Student {
private String name;
private Address address;
private String[] books;
private List<String> hobbies;
private Map<String, String> card;
private Set<String> games;
private String car;
private Properties info;
public String getName() {
return name;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", address=" + address +
", books=" + Arrays.toString(books) +
", hobbies=" + hobbies +
", card=" + card +
", games=" + games +
", wife='" + car + '\'' +
", info=" + info +
'}';
}
public void setName(String name) {
this.name = name;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public String[] getBooks() {
return books;
}
public void setBooks(String[] books) {
this.books = books;
}
public List<String> getHobbies() {
return hobbies;
}
public void setHobbies(List<String> hobbies) {
this.hobbies = hobbies;
}
public Map<String, String> getCard() {
return card;
}
public void setCard(Map<String, String> card) {
this.card = card;
}
public Set<String> getGames() {
return games;
}
public void setGames(Set<String> games) {
this.games = games;
}
public String getCar() {
return car;
}
public void setCar(String car) {
this.car = car;
}
public Properties getInfo() {
return info;
}
public void setInfo(Properties info) {
this.info = info;
}
}
(2)注入方式
<1>构造器注入
主要分为有参构造和无参构造,详情请查看上一篇文章
<2>set方式注入
1>常量注入
<property name="name" value="张三"/>
2>Bean注入
<bean id="Address" class="demo1.Address">
<property name="address" value="西安"/>
</bean>
<bean id="student" name="h1 h2" class="demo1.Student">
<!--Bean注入-->
<property name="address" ref="Address"/>
</bean>
3>数组注入
<property name="books">
<array>
<value>测试1</value>
<value>测试2</value>
<value>测试3</value>
<value>测试4</value>
</array>
</property>
4>LIst注入
<property name="hobbies">
<list>
<value>测试1</value>
<value>测试2</value>
<value>测试3</value>
<value>测试4</value>
</list>
</property>
5>Map注入
<property name="card">
<map>
<entry key="身份证号" value="123456"></entry>
</map>
</property>
6>Set注入
<property name="games">
<set>
<value>LOL</value>
</set>
</property>
7>Null注入
<property name="car">
<null></null>
</property>
8>Properties注入
<property name="info">
<props>
<prop key="学号">
2021
</prop>
</props>
</property>
测试
public class MyTest {
public static void main(String[] args) {
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
Student student = (Student) applicationContext.getBean("student");
System.out.println(student.toString());
}
}
结果
Student{name='张三', address=Address{address='西安'}, books=[测试1, 测试2, 测试3, 测试4], hobbies=[测试1, 测试2, 测试3, 测试4], card={身份证号=123456}, games=[LOL], car='null', info={学号=2021}}