【Spring 框架 03】DI 依赖注入,spring 菜鸟教程 pdf
public class AccountDao {
public void add(){
System.out.println("AccountDao.add>>>>>>>>>");
}
}
3.spring.xml 配置
标签内 name 表示传入参数的名字
标签内 ref 表示中的 id
<?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="accountDao" class="com.lcySpring.dao.AccountDao"></bean>
<bean id="accountService" class="com.lcySpring.service.AccountService">
<constructor-arg name="accountDao" ref="accountDao"></constructor-arg>
</bean>
</beans>
1.简化< property >使用 p 标签
这里我们创建一个类存在 set 方法
public class UserService {
private String username;
private int age;
public void show(){
System.out.println("姓名:"+username+" 年龄:"+age);
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
配置 spring.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
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="userService" class="com.lcySpring.service.UserService">
<property name="username" value="mlrcj"></property>
<property name="age" value="21"></property>
</bean>
</beans>
通过设置 value 属性成功传入参数
但是这样写太麻烦了我们可以使用 p 标签
在 spring.xml 中配置命名空间加入 p 插件功能(p:是写在< bean >里面的)
<?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">
<bean id="userService" class="com.lcySpring.service.UserService"
p:username="lmrcj"
p:age="21"
</bean>
</beans>
2.简化< constructor-arg >使用 c 标签
和 p 标签的功能差不多,但是它还可以提供索引赋值
<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
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="accountService" class="com.lcySpring.service.AccountService"
c:aname="aaaaaaaaaaa"
c:money="100000000000"
</bean>
<bean id="accountService02" class="com.lcySpring.service.AccountService"
c:_0="bbbbbbbbbbbbbb"
c:_1="10000000000000"
</bean>
关于 Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/c]报错
可以参考这篇文章
1.集合 list 注入
构建一个含集合的 java 类
public class CollectionService {
private List list;
p
ublic void setList(List list) {
this.list = list;
}
public void show(){
System.out.println(list);
}
}
配置 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
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="collectionService" class="com.lcySpring.service.CollectionService">
<property name="list">
<list>
<value>a</value>
<value>b</value>
<value>c</value>
<value>d</value>
</list>
</property>
</bean>
</beans>
编写测试类,检测成功注入
2.集合 set 注入
直接将 xml 中 list 配置改为 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 id="collectionService" class="com.lcySpring.service.CollectionService">
<property name="set">
<set>
<value>a</value>
<value>b</value>
<value>c</value>
<value>d</value>
</set>
</property>
</bean>
</beans>
评论