若使用有参构造创建对象
1.下标赋值
<bean id="xx" class="xx">
<constructor-arg index="0" value="xx"/>
</bean>
2.通过类型创建
<constructor-arg type="java.lang.String" value="xx"/>
3.直接通过参数来设置
<constructor-arg name="xx" value="xx"/>
在配置文件加载的时候,容器中管理的对象已经初始化了
别名:<alias name="xx" alias="xxx"/> (取另一个名,意思一样,可以通过别名获取这个对象)
id:bean 的唯一标识符,相当于对象名
class:bean 对象所对应的全限定名:包名 + 类型
name:也是别名,可以同时取多个
<bean id="xx" class="xx" name="xx,xx,xx">
import 可以将多个配置文件导入合并为一个
<import resource="xx.xml"/>
一般总的配置文件为 applicationContext.xml,使用总配置即可
依赖注入:
1.构造器注入
2. Set 方式注入
依赖:bean 对象的创建依赖于容器
注入:bean 对象中的所有属性由容器来注入
创建一个 Address 类
package com.demo.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.demo.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 +
'}';
}
}
resources 目录下创建 applicationContext.xml 文件
普通值注入 value
Bean 注入 ref
数组注入 property-array-value
List 注入 property-list-value
Map 注入 property-map-entry-key-value
Set 注入 property-set-value
Properties 注入 property-props-prop-key
<?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
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="address" class="com.demo.pojo.Address">
<property name="address" value="地址"/>
</bean>
<bean id="student" class="com.demo.pojo.Student">
<!-- 普通值注入 value -->
<property name="name" value="张三"/>
<!-- Bean注入 ref -->
<property name="address" ref="address"/>
<!-- 数组注入 property-array-value -->
<property name="books">
<array>
<value>语文</value>
<value>数学</value>
<value>英语</value>
</array>
</property>
<!-- List注入 property-list-value -->
<property name="hobbies">
<list>
<value>唱</value>
<value>跳</value>
</list>
</property>
<!-- Map注入 property-map-entry-key-value -->
<property name="card">
<map>
<entry key="身份证" value="111"/>
<entry key="银行卡" value="222"/>
</map>
</property>
<!-- Set注入 property-set-value -->
<property name="games">
<set>
<value>yuan</value>
<value>shen</value>
</set>
</property>
<!-- null -->
<property name="wife">
<null/>
</property>
<!-- Properties注入 property-props-prop-key -->
<property name="info">
<props>
<prop key="username">root</prop>
<prop key="password">root</prop>
</props>
</property>
</bean>
</beans>
test 目录下创建 MyTest 类
import com.demo.pojo.Student;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
@Test
public void test(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Student student = (Student) context.getBean("student");
System.out.println(student.toString());
}
}
3.拓展方式注入(p 命名空间注入、c 命名空间注入)
p 命名空间注入:
创建一个 User 类
package com.demo.pojo;
public class User {
private String name;
private String age;
//无参构造
public User() {
}
//有参构造
public User(String name, String age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age='" + age + '\'' +
'}';
}
}
创建 userbeans.xml 文件,引入 p 链接
xmlns:p="http://www.springframework.org/schema/p"
p 命名空间注入,可以直接注入属性的值:property
<?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"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<!-- p命名空间注入,可以直接注入属性的值:property -->
<bean id="user" class="com.demo.pojo.User" p:name="张三" p:age="18"/>
</beans>
报错去 Settings 下的 Languages -> DTDS 添加 URL
test 目录下创建 MyTest2 类
写了 User.class 就不需要强制转换
import com.demo.pojo.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest2 {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("userbeans.xml");
User user = context.getBean("user", User.class);
System.out.println(user.toString());
}
}
c 命名空间注入:
引入 c 链接,xmlns:c="http://www.springframework.org/schema/c"
通过构造器注入:construct-args
<!-- c命名空间注入,通过构造器注入:construct-args -->
<bean id="user" class="com.demo.pojo.User" c:name="张三" c:age="18"/>
注意:p 命名空间和 c 命名空间不能直接使用,需要导入 xml 约束
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"