spring使用案例 属性注入
来源:互联网 发布:notepad是什么软件 编辑:程序博客网 时间:2024/05/23 15:07
构造方法注入
1、新建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="bean" class="spring_day_2.TastDome">
<constructor-arg type="int" value="7500000"/>
</bean>
<bean id="bean2" class="spring_day_2.TastDome">
<!--name的值必须和构造方法中的参数名一致 -->
<constructor-arg name="val" value="7500000"/>
</bean>
<bean id="bean3" class="spring_day_2.TastDome">
<!--index必须和构造方法中的参数位置一致 -->
<constructor-arg index="0" value="7500000"/>
</bean>
<!-- 构造方法参数是类的实例
注意 如果声明了有参数的构造方法 那么必须自己实现一个无参数的不然会报
Failed to instantiate [spring_day_2.TastDome]: No default constructor found; nested exception is java.lang.NoSuchMethodException: spring_day_2.TastDome.<init>()
-->
<bean id="tastDome" class="spring_day_2.TastDome"/>
<bean id="bean4" class="spring_day_2.TastDome2">
<constructor-arg ref="tastDome"/>
</bean>
</beans>
2、新建对应实体类
package spring_day_2;
public class TastDome {
private int value;
public void add(){
System.out.println(value);
}
/**
* 无参数的构造
*/
public TastDome(){
}
public TastDome(int val){
this.value=val;
}
}
package spring_day_2;
public class TastDome2 {
private TastDome tastDome;
public void add(){
tastDome.add();
}
public TastDome2(TastDome tastDome ){
this.tastDome=tastDome;
}
}
3、使用
package spring_day_2;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Spring_dome {
public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext context=new ClassPathXmlApplicationContext("MyXml2.xml");
TastDome user=(TastDome) context.getBean("bean");
user.add();
user=(TastDome) context.getBean("bean2");
user.add();
user=(TastDome) context.getBean("bean3");
user.add();
TastDome2 user2=(TastDome2) context.getBean("bean4");
user2.add();
}
}
属性注入
1、新建类
package spring_day_2;
public class TastDome3 {
private TastDome tastDome;
private int val;
public void add(){
System.out.println(val);
tastDome.add();
}
public void setTastDome(TastDome tastDome) {
this.tastDome = tastDome;
}
public void setVal(int val) {
this.val = val;
}
}
2、编辑xml
<!-- set注入 -->
<bean id="bean5" class="spring_day_2.TastDome3">
<property name="tastDome" ref="tastDome"/>
<property name="val" value="1"/>
</bean>
3、使用
TastDome3 user3=(TastDome3) context.getBean("bean5");
user3.add();
p空间
1、编写xml
<bean id="tastDome2" class="spring_day_2.TastDome"/>
<bean id="bean6" class="spring_day_2.TastDome3" p:tastDome-ref="tastDome" p:val="1"/>
2、使用
user3=(TastDome3) context.getBean("bean6");
user3.add();
- spring使用案例 属性注入
- 使用Spring注解注入属性
- 使用spring注解注入属性
- spring集合注入案例
- spring setter注入案例
- Spring使用PropertyPathFactoryBean进行属性值注入
- 使用Spring注解来注入属性
- 使用Spring注解来注入属性
- 使用Spring注解来注入属性
- 关于使用spring注解注入静态属性
- spring详解:使用注解方式注入属性
- Spring--IoC--域属性的注入-使用spring注解
- Spring Boot系列 Spring @Value 属性注入使用总结一
- spring 属性注入
- spring公共属性注入
- Spring中的属性注入
- Spring属性注入
- spring 注入属性
- html标签的运用modify.html
- 惰性载入函数的使用--验证
- WebGL数据制作流程及加载
- PAT甲级1036
- html标签的运用add.html
- spring使用案例 属性注入
- 域名查询网址
- Oracle应用笔记
- 获取HDC的几种方法
- Linux下安装qt5.7.1
- 公司:在项目中导入properites,以及创建项目的时候出现gradle错误
- spark core 2.0 YarnClusterSchedulerBackend
- 如何给图片设置标签
- apt-get install nethogs安装出现E: Unmet dependencies. Try 'apt-get -f install' with no packages (or spe