构造器注入
<?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="userService" class="guhong.test1.UserService"/>
<!-- 有参构造:下标赋值 -->
<bean id="userService2" class="guhong.test1.UserService2">
<constructor-arg index="0" value="小西瓜"/>
<constructor-arg index="1" value="23"/>
</bean>
<!-- 有参构造:类型赋值 参数类型必须各不相同 不推荐-->
<bean id="userService3" class="guhong.test1.UserService3">
<constructor-arg type="java.lang.String" value="刷该"/>
<constructor-arg type="int" value="33"/>
</bean>
<!-- 有参构造:名称赋值 -->
<bean id="userService4" class="guhong.test1.UserService4">
<constructor-arg name="age" value="444"/>
<constructor-arg name="name" value="name"/>
</bean>
</beans>
setter注入
public class Student {
private String name;
private Address address;
private String[] books;
private List<String> hobbys;
private Map<String, String> cards;
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> getHobbys() {
return hobbys;
}
public void setHobbys(List<String> hobbys) {
this.hobbys = hobbys;
}
public Map<String, String> getCards() {
return cards;
}
public void setCards(Map<String, String> cards) {
this.cards = cards;
}
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 +
", books=" + Arrays.toString(books) +
", hobbys=" + hobbys +
", cards=" + cards +
", games=" + games +
", wife='" + wife + '\'' +
", info=" + info +
'}';
}
}
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="student" class="demo.guhong.test04.Student">
<!-- 普通值注入 value -->
<property name="name" value="guhong"/>
<!-- bean注入 ref -->
<property name="address" ref="address"/>
<!-- 数组 -->
<property name="books">
<array>
<value>西游记</value>
<value>水浒传</value>
<value>红楼梦</value>
<value>三国演义</value>
</array>
</property>
<!-- 列表 -->
<property name="hobbys">
<list>
<value>篮球</value>
<value>足球</value>
<value>羽毛球</value>
<value>乒乓球</value>
</list>
</property>
<!-- map -->
<property name="cards">
<map>
<entry key="公交卡" value="122343" />
<entry key="学生卡" value="1dweesdsf22343" />
<entry key="老人卡" value="43fdgdereg" />
</map>
</property>
<!-- set -->
<property name="games">
<set>
<value>CF</value>
<value>LOL</value>
<value>cs1.5</value>
</set>
</property>
<!-- null -->
<property name="wife">
<null/>
</property>
<property name="info">
<props>
<prop key="学号">12323232</prop>
<prop key="年龄">16</prop>
<prop key="性别">male</prop>
<prop key="姓名">guhong</prop>
<prop key="userName">guhong</prop>
<prop key="password">232323</prop>
</props>
</property>
</bean>
<bean id="address" class="demo.guhong.test04.Address">
<property name="address" value="亚特兰蒂斯"/>
</bean>
</beans>
p-namespace:
主要是加入: xmlns:p="http://www.springframework.org/schema/p"
如果在XML中加入上述URL,显示红色的话,请看这里
可以直接注入属性的值,使用的的无参数的构造方式
public class User {
private String name;
private int age;
public User(){
System.out.println("User 无参构造");
}
public User(String name, int age) {
System.out.println("User 有参数构造");
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 +
'}';
}
}
<?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-2.5.xsd">
<bean id="user" class="demo.guhong.test04.User" p:name="sdd" p:age="12" />
</beans>
c-namespace
加入:xmlns:c="http://www.springframework.org/schema/c"
使用的有参数的构造方法
public class Car {
private String name;
private double price;
public Car(){
System.out.println("Car 无参构造");
}
public Car(String name, double price) {
System.out.println(" Car 有参数构造");
this.name = name;
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
@Override
public String toString() {
return "Car{" +
"name='" + name + '\'' +
", price=" + price +
'}';
}
}
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:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- c命名空间注入,通过构造器注入,construct args -->
<bean id="car" class="demo.guhong.test04.Car" c:name="sdsd" c:price="12"/>
</beans>
p-namespace 和c-namesapce的结果测试
我在 User 和Car中都显示增加了无参构造和有参构造
public User(){
System.out.println("User 无参构造");
}
public User(String name, int age) {
System.out.println("User 有参数构造");
this.name = name;
this.age = age;
}
---------------------------------------------------------------
public Car(){
System.out.println("Car 无参构造");
}
public Car(String name, double price) {
System.out.println(" Car 有参数构造");
this.name = name;
this.price = price;
}
测试结果如下:
User 无参构造
Car 有参数构造
User{name='sdd', age=12}
Car{name='sdsd', price=12.0}
Process finished with exit code 0
参考:狂神说

本文深入解析Spring框架中的依赖注入技术,包括构造器注入、setter注入、p-namespace和c-namespace的使用方法及实例,帮助读者理解不同注入方式的特点与适用场景。
3644

被折叠的 条评论
为什么被折叠?



