spring中的配置
1. bean
bean就是由ioc容器实例化的对象
2. beans
beans标签用于包含bean标签
3. alias
为bean的id取别名,别名可以有多个用 ;隔开
4. import
导入其他的xml配置文件,导入后只用读取本配置文件也可使用其他文件中的bean
5. description
添加一些javabean的描述信息,用处不大
spring注入
1. Set注入(最重要)
1.1、普通值注入
通过value注入
1.2、Bean注入
注入的bean要在配置文件中已经注册
1.3、数组、List、Set注入
结构都一样,只用修改property下面的标签名称即可
1.4、Map注入
与上面三种不同的只是在map下用entry标签表示键值对
1.5、Properties
与Map注入不同的是值需要写到prop括号中
1.6、空值注入
在该属性下直接写
2. 构造器注入
- name是选择参数的名字然后用value注入
- index是参数的顺序注入,第一个参数为0
- type根据参数的类型注入
3. 扩展方式注入
在spring官网找到相应的头文件导入,然后就可以直接在bean中用c命名空间注入或者p命名空间注入
网址:https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#beans-c-namespace
- 注意:使用c命名空间注入时必须显示地定义有参、无参构造
项目结构:
实体类address:
package com.yang.pojo;
public class Address {
private String address;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Address{" +
"address='" + address + '\'' +
'}';
}
}
实体类student:
package com.yang.pojo;
import java.util.*;
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 wife;
private Properties info;
public String getName() {
return name;
}
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 getWife() {
return wife;
}
public void setWife(String wife) {
this.wife = wife;
}
public Properties getInfo() {
return info;
}
public void setInfo(Properties info) {
this.info = info;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", address=" + address.toString() +
", books=" + Arrays.toString(books) +
", hobbies=" + hobbies +
", card=" + card +
", games=" + games +
", wife='" + wife + '\'' +
", info=" + info +
'}';
}
}
实体类user:
package com.yang.pojo;
public class User {
private String name;
private int age;
public User() {
}
public User(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
核心配置文件beans.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="address" class="com.yang.pojo.Address">
<property name="address" value="南充"/>
</bean>
<bean id="student" class="com.yang.pojo.Student">
<!-- 普通值注入 value -->
<property name="name" value="秀儿"/>
<!-- bean注入 ref -->
<property name="address" ref="address"/>
<!-- 数组注入 -->
<property name="books">
<array>
<value>三国演义</value>
<value>西游记</value>
<value>水浒传</value>
<value>红楼梦</value>
</array>
</property>
<!-- List注入 -->
<property name="hobbies">
<list>
<value>听音乐</value>
<value>看电影</value>
<value>打游戏</value>
<value>敲代码</value>
</list>
</property>
<!-- Set注入 -->
<property name="games">
<set>
<value>LOL</value>
<value>CF</value>
<value>QQ飞车</value>
</set>
</property>
<!-- null -->
<property name="wife">
<null/>
</property>
<!-- Map注入 -->
<property name="card">
<map>
<entry key="饭卡" value="123"/>
<entry key="水卡" value="456"/>
<entry key="电卡" value="789"/>
</map>
</property>
<!-- Properties -->
<property name="info">
<props>
<prop key="学号">20190001</prop>
<prop key="性别">男</prop>
<prop key="姓名">秀儿</prop>
</props>
</property>
</bean>
</beans>
beans01.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- p 命名空间 注入 -->
<bean id="user" class="com.yang.pojo.User" p:age="18" p:name="秀儿"/>
<!-- c 命名空间 注入 -->
<bean id="user01" class="com.yang.pojo.User" c:age="20" c:name="秀哥"/>
<alias name="user" alias="user2;user3"/>
</beans>
测试:
import com.yang.pojo.Student;
import com.yang.pojo.User;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
@Test
public void Name(){
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Student student = (Student) context.getBean("student");
System.out.println(student);
}
@Test
public void User(){
ApplicationContext context = new ClassPathXmlApplicationContext("beans01.xml");
User user = context.getBean("user01", User.class);
System.out.println(user);
}
}
bean的作用域
1. 单例模式(默认机制)
这两个get到的是同一个对象
<bean id="student" class="com.yang.pojo.Student" scope="singleton">
2. 原型模式
这两个get到的不同的对象,每次get都会产生一个新对象
<bean id="student" class="com.yang.pojo.Student" scope="prototype">