IOC创建对象的方式
无论是否使用,只有在xml文件中被注册(bean),就会被创建。
1.用无参构造创建对象
<!--无参构造创对象
类型 变量名 = new 类型();
Hello hello =new Hello();
id = 变量名
class = new 的对象
property相当于给对象中的属性设值!
-->
<bean id="user" class="com.User.User">
<property name="name" value="陈声铭"/>
</bean>
2.用有参构造创建对象
下标赋值
<!--第一种下标赋值
index是下标,value是具体的值-->
<bean id="user" class="com.User.User">
<constructor-arg index="0" value="陈声铭"/>
</bean>
</beans>
类型赋值(不建议使用)
<!--类型赋值(不建议使用)-->
<bean id="user" class="com.User.User">
<constructor-arg type="java.lang.String" value="陈声铭"
</bean>
参数名赋值
<!--直接通过参数名来赋值-->
<bean id="user" class="com.User.User">
<constructor-arg name="name" value="陈声铭"/>
</bean>
spring的配置
起别名
<!--给user起别名叫username-->
<alias name="user" alias="username"/>
导入其他的bean文件
<!--导入其他的bean文件-->
<import resource="bean1.xml"/>
<import resource="bean2.xml"/>
<import resource="bean3.xml"/>
依赖注入
1,依赖注入(set注入)
1,依赖注入(set注入)
依赖:bean对象的创建依赖容器!
注入:bean对象中的所有属性,有容器注入!
环境搭建
1,复杂类型(Address)
package com.injection;
public class Address {
private String address;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
2.,真实测试对象
package com.injection;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Properties;
public class Student {
private String name;//字符串
private Address address;
private String[] books;
private List<String> hobbys;
private Map<String,String> card;
private Properties info;//该类主要用于读取Java的配置文件
private String wife;
private Set<String>game;
}
注入
<?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-4.3.xsd">
<bean id="address" class="com.injection.Address">
<property name="address" value="天津大学仁爱学院"/>
</bean>
<bean id="student" class="com.injection.Student">
<!--普通注入-->
<property name="name" value="陈声铭"/>
<!--bean注入-->
<property name="address" ref="address"/>
<!--数组注入-->
<property name="books">
<array>
<value>c++</value>
<value>java</value>
<value>spring</value>
<value>gradle</value>
</array>
</property>
<!--list-->
<property name="hobbys">
<list>
<value>打代码</value>
<value>打荣耀战魂</value>
<value>打DOTA2</value>
<value>学习Spring</value>
</list>
</property>
<!--map-->
<property name="card">
<map>
<entry key="农行卡" value="11111111111"/>
<entry key="饭卡" value="2222222222"/>
<entry key="建行卡" value="33333333333"/>
<entry key="身份证" value="44444444444"/>
</map>
</property>
<!--set-->
<property name="game">
<set>
<value>荣耀战魂</value>
</set>
</property>
<!--wife空值注入-->
<property name="wife">
<null/>
</property>
<!--Properties注入-->
<property name="info">
<props>
<prop key="姓名">陈声铭</prop>
<prop key="学号">6016203248</prop>
<prop key="性别">男</prop>
<prop key="长相">帅</prop>
</props>
</property>
</bean>
</beans>
结果
Student
{
name=‘陈声铭’,
address=Address{address=‘天津大学仁爱学院’},
books=[c++, java, spring, gradle],
hobbys=[打代码, 打荣耀战魂, 打DOTA2, 学习Spring],
card={农行卡=11111111111, 饭卡=2222222222, 建行卡=33333333333, 身份证=44444444444}, info={学号=6016203248, 性别=男, 长相=帅, 姓名=陈声铭},
wife=‘null’
}
p命名空间注入(属性注入,用无参构造器)
<!--p命名空间注入,可以注入简单值,用无参构造器-->
<bean id ="user" class="com.injection.User" p:name="陈声铭" p:age="22"/>
c命名空间注入(构造器注入,用有参构造器)
<!--p命名空间注入,可以注入简单值,用有参构造器-->
<bean id ="user2" class="com.injection.User" c:name="雷志明" c:age="78"/>
测试
import com.injection.Student;
import com.injection.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
public static void main(String[] arsg){
ApplicationContext context2 = new ClassPathXmlApplicationContext("Userbean.xml");
User user = context2.getBean("user",User.class);
System.out.println(user.toString());
ApplicationContext context3 = new ClassPathXmlApplicationContext("Userbean.xml");
User user2 = context2.getBean("user2",User.class);
System.out.println(user2.toString());
}
}
结果
注解注入
package com.injection;
import org.springframework.stereotype.Component;
//Component等价于<bean id ="user" class="com.injection.User"/>
@Component
public class User {
private String name="陈声铭";
@Value("陈声明")//相当property name2="name" value="陈声明2"/>
private String nam2;
private String name;
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
'}';
}
}
和@Component功能一样的还有四个,都是将某个类注册到Spring,装配Bean。
一般用在dao层【@Repository】
一般用在service层【@Service】
一般用在controller层【@Controller】
构造器注入
注入类User(构造函数,set函数)
@Data
public class User{
private String name;
private int age;
public User(String name,int age){
this.name = name;
this.age = age;
System.out.println("有参构造方法调用");
}
public User(){
this.name = null;
this.age = 0;
System.out.println("无参构造方法调用");
}
}
注入类的配置(构造函数,set函数)
<?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 name="User" class="com.pojo.User">
<property name="name" value="陈声铭"/>
<property name="age" value="23"/>
</bean>
</beans>
测试
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("UserBean.xml");
User user = (User)applicationContext.getBean("User");
System.out.println(user);
}