1、构造器注入
之前的博客里面写了!就是使用下面这种constructor-arg来实现注入!
注意:构造器注入时,实体类必须要有有参构造!
<bean id="user" class="lin.pojo.User" name="u,new_user">
<constructor-arg name="name" value="林XX"/>
</bean>
2、Set方式注入【重点】
依赖注入本质是Set注入!
依赖:bean对象的创建依赖于容器
注入:bean对象中的所有属性,由容器来注入
Student类:
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student {
private String name;
private Address address;
private String[] books;
private List<String> hobbys;
private Map<String,String> card;
private Set<String> games;
private String wife;
private Properties info;//配置类
}
Address类:
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Address {
private String address;
}
applicationContext.xml文件: (Set注入的8种不同类型的方法!)
<?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="lin.pojo.Address">
<property name="address" value="浙江杭州"></property>
</bean>
<bean id="student" class="lin.pojo.Student">
<!--1:普通值注入,使用value-->
<property name="name" value="林XX"/>
<!--2:bean注入,使用ref-->
<property name="address" ref="address"/>
<!--3:数组注入-->
<property name="books">
<array>
<value>Java</value>
<value>SpringBoot</value>
<value>J2EE</value>
<value>JavaScript</value>
</array>
</property>
<!--4:List注入-->
<property name="hobbys">
<list>
<value>篮球</value>
<value>足球</value>
<value>羽毛球</value>
<value>排球</value>
</list>
</property>
<!--5:Map注入-->
<property name="card">
<map>
<entry key="身份证" value="330327XXXXXXXXXXXX"></entry>
</map>
</property>
<!--6:Set注入-->
<property name="games">
<set>
<value>王者荣耀</value>
<value>原神</value>
</set>
</property>
<!--7:null注入-->
<property name="wife" value=""/>
<!--8:Properties注入-->
<property name="info">
<props>
<prop key="driver"></prop>
<prop key="url"></prop>
<prop key="username">root</prop>
<prop key="password">123456</prop>
</props>
</property>
</bean>
</beans>
测试类:
public class MyTest {
public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext("applicationContext.xml");
Student student = (Student) context.getBean("student");
System.out.println(student);
}
}
结果:
Student(name=林XX, address=Address(address=浙江杭州), books=[Java, SpringBoot, J2EE, JavaScript], hobbys=[篮球, 足球, 羽毛球, 排球], card={身份证=330327XXXXXXXXXXXX}, games=[王者荣耀, 原神], wife=, info={password=123456, url=, driver=, username=root})
3、拓展方式注入(C命名和P命名空间注入)
1)p命名空间:对应的就是Set注入的属性注入
不能直接使用先要加上 xmlns:p="http://www.springframework.org/schema/p" 约束
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--p 命名空间注入,可以直接注入属性的值-->
<bean id="user" class="lin.pojo.User" p:age="18" p:name="林XX"/>
</beans>
@Test
public void test2(){
ApplicationContext context =
new ClassPathXmlApplicationContext("userbean.xml");
User user = (User) context.getBean("user");
System.out.println(user);
}
2)c命名空间:对应的就是构造器注入注入(constructor-arg)
不能直接使用先要加上 xmlns:c="http://www.springframework.org/schema/c" 约束
<?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 http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--p 命名空间注入,可以直接注入属性的值-->
<bean id="user" class="lin.pojo.User" p:age="18" p:name="林XX"/>
<!--c 命名空间注入,构造器注入-->
<bean id="user2" class="lin.pojo.User" c:age="18" c:name="林XX"/>
</beans>
@Test
public void test3(){
ApplicationContext context =
new ClassPathXmlApplicationContext("userbean.xml");
User user = (User) context.getBean("user2");
System.out.println(user);
}