(二)spring的注入---配置构造器属性注入
2017-10-18 20:54
435 查看
第一步:创建一个类Car
第二步:配置bean
第三步:运行main方法
运行结果:
Car [company=Audi, brand=ShangHai, maxSpeed=300, price=0.0]`
package com.atguigu.spring.beans; public class Car { private String company; private String brand; private int maxSpeed; private float price; public String getCompany() { return company; } public void setCompany(String company) { this.company = company; } public String getBrand() { return brand; } public void setBrand(String brand) { this.brand = brand; } public int getMaxSpeed() { return maxSpeed; } public void setMaxSpeed(int maxSpeed) { this.maxSpeed = maxSpeed; } public float getPrice() { return price; } public void setPrice(float price) { this.price = price; } public Car(String company, String brand, float price) { super(); this.company = company; this.brand = brand; this.price = price; } public Car(String company, String brand, int maxSpeed) { super(); this.company = company; this.brand = brand; this.maxSpeed = maxSpeed; } @Override public String toString() { return "Car [company=" + company + ", brand=" + brand + ", maxSpeed=" + maxSpeed + ", price=" + price + "]"; } }
第二步:配置bean
<!--通过构造方法来配置bean的属性 --> <bean id="car" class="com.atguigu.spring.beans.Car"> <constructor-arg value="Audi"></constructor-arg> <constructor-arg value="ShangHai"></constructor-arg> <constructor-arg value="300"></constructor-arg> </bean>
第三步:运行main方法
Car car=(Car) ctx.getBean("car"); System.out.println(car);
运行结果:
Car [company=Audi, brand=ShangHai, maxSpeed=300, price=0.0]`
相关文章推荐
- [原创]java WEB学习笔记98:Spring学习---Spring Bean配置及相关细节:如何在配置bean,Spring容器(BeanFactory,ApplicationContext),如何获取bean,属性赋值(属性注入,构造器注入),配置bean细节(字面值,包含特殊字符,引用bean,null值,集合属性list map propert),util 和p 命名空间
- spring xml 配置 属性注入 构造方法注入
- Spring在配置文件中的属性注入
- spring 将配置文件中的值注入 属性
- Spring AOP注解通过@Autowired,@Resource,@Qualifier,@PostConstruct,@PreDestroy注入属性的配置文件详解
- Spring属性注入和构造器注入、工厂方法注入
- Spring根据XML配置文件注入属性的方法
- Spring 配置bean, 注解方式配置 xml方式要生产set方法方便属性注入,注解方式不需要,利用反射机制注入
- SPRING.NET 配置对象属性注入的例子
- Spring AOP注解通过@Autowired,@Resource,@Qualifier,@PostConstruct,@PreDestroy注入属性的配置文件详解
- Spring AOP注解通过@Autowired,@Resource,@Qualifier,@PostConstruct,@PreDestroy注入属性的配置文件详解
- Spring中使用Map、Set、List、数组、属性集合的注入方法配置文件
- Spring使用注解的方式注入属性时的xml文件配置
- Spring AMQP实例 以及Spring 配置文件动态注入属性使用实战
- spring中属性文件和配置文件的注入方式
- spring的两种属性注入方式setter注入和构造器注入或者自动注入
- Spring AOP注解通过@Autowired,@Resource(J2EE提供),@Qualifier,@PostConstruct,@PreDestroy注入属性的配置文件详解
- Spring中使用事务搭建转账环境方法二 相对简便的注解方法 ——配置文件注入对象属性需要setter方法 注解方法,不需要生成setter方法
- Spring中使用Map、Set、List、数组、属性集合的注入方法配置文件
- Spring AOP注解通过@Autowired,@Resource,@Qualifier,@PostConstruct,@PreDestroy注入属性的配置文件详解