Spring注入方式(1)

摘要:
Spring支持三种依赖注入方法,即属性注入、构造函数注入和工厂方法注入(很少使用,不推荐)。下面详细描述属性注入和构造函数注入。1.常量注入属性注入是通过setter方法注入bean属性值,属性注入使用<属性>元素,使用name属性指定bean的属性名称,并使用value属性或<值>子节点指定属性值。豆类的主要成分。xml文件

      Spring支持3种依赖注入方式,分别为属性注入、构造器注入和工厂方法注入(很少使用,不推荐),下面分别对属性注入和构造器注入详细讲解。

1、常量注入

  属性注入是通过setter方法注入Bean的属性值,属性注入使用<property>元素,使用name属性指定Bean的属性名称,使用value属性或者<value>子节点指定属性值。

beans.xml文件主要内容

<!--通过属性注入方式配置  -->
<bean   class="com.spring.Person">
    <property name="name" value="Tom"></property>
    <property name="age" value="29"></property>
</bean>

Person.java

package com.spring;
public class Person {
	private String name;
	private int age;
	private double height;
	private double weight;
	
	public Person(String name, int age, double height, double weight) {
		super();
		this.name = name;
		this.age = age;
		this.height = height;
		this.weight = weight;
	}
	
	public Person() {
		super();
	}
	
	
	public double getHeight() {
		return height;
	}

	public void setHeight(double height) {
		this.height = height;
	}

	public double getWeight() {
		return weight;
	}

	public void setWeight(double weight) {
		this.weight = weight;
	}

	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 "Person [name=" + name + ", age=" + age + ", height=" + height + ", weight=" + weight + "]";
	}
	
	
	
	
}

Main.java

package com.spring;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {
	public static void main(String[] args) {
		ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
		Person person = (Person) ctx.getBean("person");
		System.out.println(person);
} }

运行结果截图如下:

Spring注入方式(1)第1张

以上就是属性注入的例子

2、构造器注入

2.1 按照索引匹配

beans.xml

<bean   class="com.spring.Person">
		<constructor-arg value="Tim" index="0"></constructor-arg>
		<constructor-arg value="30" index="1"></constructor-arg>
		<constructor-arg value="30" index="3"></constructor-arg>
		<constructor-arg value="65" index="2"></constructor-arg>
		
</bean>

结果:

Spring注入方式(1)第2张

2.2 按类型匹配

beans.xml

Spring注入方式(1)第3张Spring注入方式(1)第4张
1 <bean   class="com.spring.Person">
2         <constructor-arg value="34" type="int"></constructor-arg>
3         <constructor-arg value="175" type="double"></constructor-arg>
4         <constructor-arg value="Jerry" type="java.lang.String" ></constructor-arg>
5         <constructor-arg value="70" type="double"></constructor-arg>
6     </bean>
View Code

结果:

Spring注入方式(1)第5张

免责声明:文章转载自《Spring注入方式(1)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale"MYSQL管理----数据库删除恢复下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

spring mvc Response header content type

Xml代码 <bean >      <property name="messageConverters">    <list>     <ref bean="mappingJacksonHttpMessageConverter" /><!-- json转换器 -->    </list&g...

Spring boot validation校验

使用 Hibernate validator 的步骤:1. 在 Pojo 类的字段上, 加上 Hibernate validator 注解2. 在Controller 函数的形参前加上 @Valid 或 @Validated 注解, 触发一次validation. 3. 在每个 @Valid 或 @Validated 注解参数后, 紧跟一个 Binding...

Failed to bind properties under 'spring.datasource' to javax.sql.DataSource:

Springboot在更换数据源的为druid的时候,报错: Description: Failed to bind properties under 'spring.datasource'to javax.sql.DataSource: Property: spring.datasource.filters Value: stat,wall,log4...

spring:org.springframework.web.servlet.DispatcherServlet noHandlerFound解决方法

1.搜了许久: <servlet-mapping><servlet-name>dispatcher</servlet-name><url-pattern>/*</url-pattern></servlet-mapping>要去掉*spring用到forward("/WEB-INF/js...

SSH三大框架整合步骤

Struts2:需要整合的第一个框架: 1.创建一个动态web项目 2.导入struts2必须的jar 放到 lib目录下 ,再 build path 添加web工程中 3.配置struts2的核心配置文件:struts.xml 4.在web.xml文件中添加struts2的核心过滤器 5.添加测试页面 6.导入tomcat jar包 对j2e...

扩展spring mvc的拦截器,实现AOP的环绕增加效果

原因: 1. spring mvc拦截器通过中postHander方法中只有ModelAndView类型的结果,如果@Controller返回的是@ResponseBody的字符串类型,ModelAndView的值就为null,就不能在postHandler中把结果写入日志或做其它对结果的处理。 public void postHandle( Htt...