Spring基础知识(2)

一、Spring的Bean属性的依赖注入

      为一个Bean设置属性,有三种注入属性的方式:

      1)接口注入

      2)构造器注入

      3)setter方法注入

      详细介绍如下:

      接口注入:

      public   interface   Injection{

            public   void   injectName(String   name);

      }

      为对象注入name属性

      public   class   User   implements   Injection{

            public void injectName(String name){

                  this.name = name;

            }

            private String name;

      }


      构造器注入:

      public class User{

            private String name;

            public User(String name){

                  this.name = name;

            }

      }

      

      setter方法注入:

      public class User{

            private String name;

            public void setName(String name){

                  this.name = name;

            }

      }

      ☆:Spring框架支持构造器注入和setter方法注入。


Spring中属性的依赖注入:

      第一种:构造器注入,通过 <constructor-arg> 元素完成注入

public class Car {
	private String name;
	private double price;
	
	public Car(String name, double price){
		super();
		this.name = name;
		this.price = price;
	}

	@Override
	public String toString() {
		return "Car [name=" + name + ", price=" + price + "]";
	}
	
}

	<!-- 构造器注入 -->
	<bean id="car" class="lsq.spring.c_di.Car">
		<!-- 通过构造器参数,完成属性的注入 -->
		<constructor-arg index="0" type="java.lang.String" value="奥迪"></constructor-arg>
		<constructor-arg index="1" type="double" value="500000"></constructor-arg>
	</bean>
      第二种:setter方法注入, 通过<property> 元素完成注入    

public class Car2 {
	private String name;
	private double price;
	
	//注入属性时,只需要提供set方法
	public void setName(String name) {
		this.name = name;
	}
	public void setPrice(double price) {
		this.price = price;
	}
	
	@Override
	public String toString() {
		return "Car2 [name=" + name + ", price=" + price + "]";
	}
}

	<!-- setter方法注入 -->
	<bean id="car2" class="lsq.spring.c_di.Car2">
		<!-- 通过property元素完成属性注入 -->
		<property name="name" value="JEEP"></property>
		<property name="price" value="200000"></property>
	</bean>
      * 使用 <property> 元素 ref属性,引入另一个Bean对象,完成Bean之间注入 

public class Employee {
	private String name;
	private Car2 car2;
	
	public void setName(String name) {
		this.name = name;
	}
	
	public void setCar2(Car2 car2) {
		this.car2 = car2;
	}
	
	@Override
	public String toString() {
		return "Employee [name=" + name + ", car2=" + car2 + "]";
	}
	
}

	<bean id="employee" class="lsq.spring.c_di.Employee">
		<property name="name" value="wade"></property>
		<property name="car2" ref="car2"></property>
	</bean>
      以上逻辑的测试代码如下:

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * 完成依赖注入的测试
 *
 * @author lishanquan
 *
 */
public class DITest {

	@Test
	public void demo1(){
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
		Car car = (Car) applicationContext.getBean("car");
		System.out.println(car);
	}
	
	@Test
	public void demo2(){
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
		Car2 car2 = (Car2) applicationContext.getBean("car2");
		System.out.println(car2);
	}
	
	@Test
	public void demo3(){
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
		Employee employee = (Employee) applicationContext.getBean("employee");
		System.out.println(employee);
	}
}

二、* 名称空间 p的使用  (Spring2.5 新特性)

      spring2.5版本 引入名称空间p, 简化属性注入的配置
      p:<属性名>="xxx" 引入常量值
      p:<属性名>-ref="xxx" 引用其它Bean对象

      具体使用步骤:

      1) 引入p名称空间

      

      2)改写<property>注入为 p名称空间注入
      

      修改为如下所示:

      

      测试结果相同。


三、* spring3.0之后引入 spEL 表达式

      1、完成对象之间注入 

      

      修改为

      

      

      2、使用另一个Bean属性完成注入     

<span style="white-space:pre">	</span><bean id="carInfo" class="cn.itcast.spring.e_di.CarInfo"></bean>
	<bean id="car2_2" class="cn.itcast.spring.e_di.Car2">
		<property name="name" value="#{carInfo.name}"></property>
	</bean>
      3、 使用另一个Bean方法完成注入

	<bean id="carInfo" class="cn.itcast.spring.e_di.CarInfo"></bean>
	<bean id="car2_2" class="cn.itcast.spring.e_di.Car2">
		<property name="name" value="#{carInfo.name}"></property>
		<property name="price" value="#{carInfo.caculatePrice()}"></property>
	</bean>

 四、集合属性的注入 

          Spring提供专门标签完成List、Set、Map、Properties等集合元素属性注入。

      1) 注入List (数组)   

import java.util.List;

public class CollectionBean {
	private List hobbies;

	public void setHobbies(List hobbies) {
		this.hobbies = hobbies;
	}

	@Override
	public String toString() {
		return "CollectionBean [hobbies=" + hobbies + "]";
	}
	
}
	<!-- 集合属性注入 -->
	<bean id="collectionBean" class="lsq.spring.c_di.CollectionBean">
		<property name="hobbies">
			<list>
				<!-- <value>标签注入基本类型,<ref/>注入复杂类型 -->
				<value>体育</value>
				<value>音乐</value>
			</list>
		</property>
	</bean>
      2)注入Set

	<property name="numbers">
			<set>
				<value>10</value>
				<value>6</value>
				<value>15</value>
			</set>
	</property>
      3)注入Map
	<property name="map">
			<map>
<span style="white-space:pre">			</span><!-- 复杂类型	<entry key-ref="" value-ref=""></entry> -->
				<entry key="name" value="wade"></entry>
				<entry key="address" value="迈阿密"></entry>
			</map>
	</property>

      4)注入Properties

      * java.utils.Properties 类 继承  java.utils.HashTable 

      Properties key和value都是String类型。

      例如:

	<property name="properties">
		<props>
			<prop key="company">NBA</prop>
			<prop key="pnum">100</prop>
		</props>
	</property>

五、在Spring框架中引入多个XML配置文件

      1、并列引入多个XML

   ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans1.xml", "beans2.xml");
      2、引入总xml文件,在总xml文件引入 子xml文件

<span style="white-space:pre">	</span>ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
<span style="white-space:pre">	</span>* 在applicationContext.xml 中 
	<import resource="classpath:bean1.xml"/>
	<import resource="classpath:bean2.xml"/>
      在开发中主要使用 第二种 , 将配置文件分离配置 便于维护管理 












  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java Spring是一个广泛使用的轻量级开源框架,用于构建企业级Web应用程序。Spring框架提供了模块化的解决方案,简化了企业级应用的开发、管理以及依赖注入(Dependency Injection,DI)等功能。以下是一些Spring基础知识点的整理: 1. **IoC(Inversion of Control)和DI(Dependency Injection)**:Spring的核心思想就是IoC,它反转了传统的控制流动,使对象之间通过容器管理彼此的依赖关系,而不是硬编码。DI是IoC的一种具体实现方式,通过配置文件或注解自动为对象提供所需依赖。 2. **Bean的作用域和生命周期**:Spring中的Bean有多种作用域,如Singleton(单例)、Prototype(原型)、Request、Session等。每个Bean都有其生命周期,从创建、初始化到使用和销毁。 3. **Spring配置文件**:通常使用XML配置文件(如applicationContext.xml)或Java配置(@Configuration classes)来定义Spring应用的组件和依赖关系。 4. **AOP(Aspect Oriented Programming)**:Spring AOP支持面向切面编程,可以编写跨组件的行为,比如日志记录、事务管理等。 5. **Spring MVC**:Spring提供的web MVC架构,包括Controller处理HTTP请求,Model负责数据访问和业务逻辑,View负责渲染结果给用户。 6. **Spring Boot**:Spring Boot简化了Spring应用的初始搭建,自动配置了许多常用的功能,使得快速开发变得更容易。 7. **Spring Data**:提供了一套高级API,用于简化数据访问操作,如JPA、MongoDB等。 8. **Spring Security**:用于实现Web应用的安全管理,包括认证、授权、会话管理等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值