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">
通过设置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”
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 id=“accountService02” class=“com.lcySpring.service.AccountService”
c:_0=“bbbbbbbbbbbbbb”
c:_1=“10000000000000”
关于Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/c]报错
可以参考这篇文章
1.集合list注入
构建一个含集合的java类
public class CollectionService {
private List list;
public 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">
a
b
c
d
编写测试类,检测成功注入
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">
a
b
c
d
3.map类型属性注入
在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">
lmr
菜鸡
小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数初中级Java工程师,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年最新Java开发全套学习资料》送给大家,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频
如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注Java)
整理了一份《2024年最新Java开发全套学习资料》送给大家,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。**
[外链图片转存中…(img-vOMVZXIi-1710850879886)]
[外链图片转存中…(img-gQsJfOtt-1710850879886)]
[外链图片转存中…(img-y0w78Hde-1710850879887)]
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频
如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注Java)
[外链图片转存中…(img-w50Qybpj-1710850879887)]