方法一,下标赋值
<bean id="user" class="edu.dj.pojo.User">
<constructor-arg index="0" value="D J"/>
</bean>
第二种,根据属性类型赋值,不建议使用,可能会存在相同类型的属性
<bean id="user" class="edu.dj.pojo.User">
<constructor-arg type="java.lang.String" value="丁 杰"/>
</bean>
第三种,直接通过参数名传值
<bean id="user" class="edu.dj.pojo.User">
<constructor-arg name="name" value="DingJie"/>
</bean>
<!--别名,如果添加了别名,我们也可以通过别名来获取对象-->
<alias name="user" alias="nancy"/>
<!--
id:bean的唯一标识符,相当于对象名
class:bean所对应的全限定名:包名+类型
name:name也是别名,而且name可以取多个别名
-->
<bean id="useT" class="edu.dj.pojo.UserT" name="user2,user3;user4 user5">
<property name="name" value="Spring学习"/>
</bean>
import一般用于团队开发,他可以将多个配置文件,导入合并为一个
假设,现在项目中有多个人开发,这三个人复制不同的类开发,不同的类注册在不同的bean中,我们可以利用
import将所有人的bean.xml文件合并为一个总的
<import resource="beans.xml"/>
<import resource="beans2.xml"/>
<import resource="beans3.xml"/>
前面已经说过
【环境搭建】
public class Address {
private String address;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
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;
}
<?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="edu.dj.pojo.Address">
<property name="address" value="中国"/>
</bean>
<bean id="student" class="edu.dj.pojo.Student">
<!--1. 简单的数据注入 value-->
<property name="name" value="丁杰"/>
<!--2. 自定义数据类型注入 ref-->
<property name="address" ref="address"/>
<!--3. 数组注入 array value-->
<property name="books">
<array>
<value>红楼梦</value>
<value>西游记</value>
<value>三国演义</value>
<value>水浒传</value>
</array>
</property>
<!--4. List注入 list value-->
<property name="hobbies">
<list>
<value>看视频</value>
<value>学python</value>
<value>学Java</value>
</list>
</property>
<!--5. Map注入 map entry-->
<property name="card">
<map>
<entry key="身份证" value="342423423432423234"/>
<entry key="银行卡" value="342423423432423234"/>
</map>
</property>
<!--6. Set注入 set value-->
<property name="games">
<set>
<value>LOL</value>
<value>DotA</value>
<value>COC</value>
<value>BOB</value>
</set>
</property>
<!--7. null注入 -->
<property name="wife">
<null/>
</property>
<!--8. Properties注入 -->
<property name="info">
<props>
<prop key="学号">18488314</prop>
<prop key="性别">男</prop>
<prop key="籍贯">江苏</prop>
</props>
</property>
</bean>
</beans>
public class MyTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Student student = (Student) context.getBean("student");
System.out.println(student.toString());
}
}
使用p命名空间和c命名空间方式进行注入
<!-- p命名空间输入,可以直接注入属性的值:properties -->
<bean id="user" class="edu.dj.pojo.User" p:name="丁杰" p:age="18"/>
<!-- c命名空间输入,可以通过构造器注入:construct-args -->
<bean id="user2" class="edu.dj.pojo.User" c:age="18" c:name="天啊"/>
注意:使用p命名和c命名空间注入需要添加约束
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
<bean id="user2" class="edu.dj.pojo.User" c:age="18" c:name="天啊" scope="singleton"/>
<bean id="user2" class="edu.dj.pojo.User" c:age="18" c:name="天啊" scope="prototype"/>
在Spring中有三种自动装配的方式
环境搭建:一个人有两个宠物
<!-- 自动装配 byName:会自动在容器的上下文中查找,和自己对象set方法后面的值对应的bean id-->
<bean id="cat" class="edu.dj.pojo.Cat"/>
<bean id="dog" class="edu.dj.pojo.Dog"/>
<bean id="person" class="edu.dj.pojo.Person" autowire="byName">
<property name="name" value="我是你爸爸"/>
</bean>
<bean id="cat" class="edu.dj.pojo.Cat"/>
<bean id="dog" class="edu.dj.pojo.Dog"/>
<!-- 自动装配 byName:会自动在容器的上下文中查找,和自己对象属性类型相同的bean-->
<bean id="person" class="edu.dj.pojo.Person" autowire="byType">
<property name="name" value="我是你爸爸"/>
</bean>
小结:
使用注解须知:
1.导入约束
2.配置注解的支持:context:annotation-config/
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config/>
</beans>
@Autowired
直接在属性上使用即可,也可以在set方法上使用!
使用Autowired我们可以不编写Set方法,前提是自动装配的属性在IOC(Spring)容器中存在,且符合名字byName
科普:
@Nullable 字段标记了这个注解,说明这个字段可以为null;
public @interface Autowired {
boolean required() default true;
}
测试代码:
public class Person {
@Autowired
private Cat cat;
//如果显示定义了Autowired的require属性为false,说明这个对象可以为null,否则不允许为空
@Autowired(required = false)
private Dog dog;
private String name;
}
如果@Autowired自动装配环境比较复杂,自动装配无法通过一个注解【@Autowired】完成时,我们可以使用@Qualifier(value="xx")去配置@Autowired的使用,指定一个唯一的bean对象注入!
public class Person {
@Autowired
@Qualifier(value = "cat")
private Cat cat;
@Autowired
@Qualifier(value = "dog")
private Dog dog;
private String name;
}
@Resource注解
public class Person {
@Resource(name = "cat2")
private Cat cat;
@Resource
private Dog dog;
private String name;
}
@Resource和Autowired的区别:
原文:https://www.cnblogs.com/jsit-dj-it/p/14898930.html