这一节我们介绍一下Spring框架的相关常用配置

  • Spring依赖注入的两种方式(构造方法注入和setter方式注入)
  • p-namespace方式配置
  • properties属性文件配置方式
  • 集合对象配置方式
  • Bean scopes作用域(单例作用域和原生作用域)

1. Spring依赖注入方式

  • 构造方法注入,它相当于在Spring初始化对象的时候调用构造方法将其对象之间的依赖关系给注入到对象中

    • 先在类中定义好依赖对象
    • 再去定义构造方法,通过在构造方法的参数中设置对象的依赖关系
    • 最后在Spring配置文件中使用<constructor-arg>标签搞定对象的依赖注入
package com.gxa.spring.day02;

public class PetServiceImpl {

    private PetDaoImpl petDao; //依赖对象

    public PetServiceImpl(PetDaoImpl petDao) { //构造方法的DI
this.petDao = petDao;
} public void selectPet() {
petDao.selectPet();
}
}
package com.gxa.spring.day02;

public class PetDaoImpl {

    public void selectPet() {
/**
* 完成宠物数据查询
*/
System.out.println("==宠物数据查询==");
}
}
<?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="petService" class="com.gxa.spring.day02.PetServiceImpl">
<constructor-arg name="petDao" ref="petDao"></constructor-arg>
</bean> <bean id="petDao" class="com.gxa.spring.day02.PetDaoImpl"></bean> </beans>
  • 设值注入,它通过给依赖对象添加setter方法来完成对象的DI

    • 先定义好依赖对象
    • 再给依赖对象添加setter方法
    • 最后在配置文件中使用<property.../>标签就OK了
package com.gxa.spring.day01;

public class PetServiceImpl {

    private PetDaoImpl petDao; //依赖对象
private ItemDaoImpl itemDao; //依赖对象 public void setPetDao(PetDaoImpl petDao) {
this.petDao = petDao;
} public void setItemDao(ItemDaoImpl itemDao) {
this.itemDao = itemDao;
} public void selectPet() {
petDao.selectPet();
itemDao.selectItem();
}
}
package com.gxa.spring.day01;

public class PetDaoImpl {

    public void selectPet() {
/**
* 完成宠物数据查询
*/
System.out.println("==宠物数据查询==");
}
}
package com.gxa.spring.day01;

public class ItemDaoImpl {

    public void selectItem() {
/**
* 完成宠物分类数据查询
*/
System.out.println("==宠物分类的数据查询==");
}
}
<?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="person" class="com.gxa.spring.day01.Person"></bean> <bean id="petService" class="com.gxa.spring.day01.PetServiceImpl">
<property name="petDao" ref="petDao"></property>
<property name="itemDao" ref="itemDao"></property>
</bean> <bean id="petDao" class="com.gxa.spring.day01.PetDaoImpl"></bean> <bean id="itemDao" class="com.gxa.spring.day01.ItemDaoImpl"></bean>
</beans>

2. p-namespace配置方式

  • 主要去简化<property>标签的配置
  • 要使用p-namespace需要在整个配置文件声明部分加入p-namespace的XMLSchema定义
<?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="petService" class="com.gxa.spring.day02.PetServiceImpl">
<constructor-arg name="petDao" ref="petDao"></constructor-arg>
</bean> <bean id="petDao" class="com.gxa.spring.day02.PetDaoImpl"></bean> <bean id="person" class="com.gxa.spring.day02.Person" p:petDao-ref="petDao"></bean> </beans>

3. properties属性文件配置,此配置可以利用Spring框架帮我们解析Java中的属性文件。下面我们介绍两种配置方法来解析Java中的属性文件

  • <bean class=”org.springframework.beans.factory.config.PropertyPlaceholderConfiguer”>来解析Java中属性文件
<?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 class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations" value="jdbc.properties"></property>
</bean> <bean id="dbConnection" class="com.gxa.spring.day02.DBConnection"
p:username="${mysql.username}"
p:password="${mysql.password}"
p:driver="${mysql.driver}"></bean> </beans>
  • <context:property-placeholder location=”jdbc.properties”>来解析Java中属性文件。这个需要在Spring配置文件的XMLSchema导入xmlns:context
<?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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"> <context:property-placeholder location="jdbc.properties"/> <bean id="dbConnection" class="com.gxa.spring.day02.DBConnection"
p:username="${mysql.username}"
p:password="${mysql.password}"
p:driver="${mysql.driver}"></bean> </beans>

4. 集合对象的配置

  • 在Spring框架配置文件中采用<list>,<Map>,<Set>,<Props>,可以帮我们来为集合对象进行对象初始化的工作。大家重点关注下面的代码
<?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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"> <bean id="collectionsSpring" class="com.gxa.spring.day02.CollectionsSpring" scope="prototype">
<property name="list">
<list>
<value>Hello World</value>
<ref bean="dbConnection"/>
</list>
</property>
<property name="map">
<map>
<entry key="key01" value="Hello World"></entry>
<entry key="key02" value-ref="dbConnection"></entry>
</map>
</property>
<property name="set">
<set>
<value>Hello World</value>
<ref bean="dbConnection"/>
</set>
</property>
<property name="props">
<props>
<prop key="key01">Hello World</prop>
<prop key="key02">liuyang</prop>
</props>
</property>
</bean> </beans>
package com.gxa.spring.day02;

import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set; public class CollectionsSpring {
private List<?> list;
private Map<String,?> map;
private Set<?> set;
private Properties props; public void setList(List<?> list) {
this.list = list;
} public void setMap(Map<String, ?> map) {
this.map = map;
} public void setSet(Set<?> set) {
this.set = set;
} public void setProps(Properties props) {
this.props = props;
} public void showList() {
for (int i = 0; i <list.size(); i++) {
System.out.println(list.get(i));
}
} public void showMap() {
System.out.println(map);
} public void showSet() {
Iterator<?> iterator = set.iterator();
while(iterator.hasNext()) {
System.out.println(iterator.next());
}
} public void showProps() {
Enumeration<?> enumeration = props.propertyNames();
while(enumeration.hasMoreElements()) {
System.out.println(props.getProperty(enumeration.nextElement().toString()));
}
}
}

5. Bean Scopes作用域

  • Singleton:单例作用域,Spring容器初始化对象只有唯一个(默认)
  • Prototype:原生作用域,每次调用Spring容器的getBean方法都会重新产生一个新的对象
  • Request
  • Session
  • Global Session

这里重点还是谈一下单例作用域和原生作用域。在配置单例作用域和原生作用域需要使用scope属性

<?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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"> <bean id="collectionsSpring" class="com.gxa.spring.day02.CollectionsSpring" scope="prototype">
<property name="list">
<list>
<value>Hello World</value>
<ref bean="dbConnection"/>
</list>
</property>
<property name="map">
<map>
<entry key="key01" value="Hello World"></entry>
<entry key="key02" value-ref="dbConnection"></entry>
</map>
</property>
<property name="set">
<set>
<value>Hello World</value>
<ref bean="dbConnection"/>
</set>
</property>
<property name="props">
<props>
<prop key="key01">Hello World</prop>
<prop key="key02">liuyang</prop>
</props>
</property>
</bean> </beans>
package com.gxa.spring.test02;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.gxa.spring.day02.CollectionsSpring;
import com.gxa.spring.day02.DBConnection;
import com.gxa.spring.day02.Person;
import com.gxa.spring.day02.PetServiceImpl; public class Test01 { @Test
public void m05() {
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
CollectionsSpring collectionsSpring01 = context.getBean("collectionsSpring", CollectionsSpring.class);
CollectionsSpring collectionsSpring02 = context.getBean("collectionsSpring", CollectionsSpring.class);
System.out.println(collectionsSpring01.hashCode());
System.out.println(collectionsSpring02.hashCode());
} }

[刘阳Java]_Spring相关配置介绍_第5讲的更多相关文章

  1. [刘阳Java]_Spring常用注解介绍_第6讲

    Spring的注解是在Spring2.5的版本中引入的,目的简化XML配置.在企业开发过程中使用注解的频率非常高,但是学习注解的前提是大家一定要对Spring基于XML配置要熟悉,这是我个人建议,因为 ...

  2. [刘阳Java]_MyBatis_实体关系映射_第8讲

    MyBatis既然是一个ORM框架,则它也有像Hibernate那样的一对多,多对多,多对一的实体关系映射功能.下面我们就来介绍一下如何使用MyBatis的实体关系映射 1.MyBatis实体关系映射 ...

  3. [刘阳Java]_BeanNameViewResolver视图解析器_第8讲

    BeanNameViewResolver:这个视图解析器跟XmlViewResolver有点类似,也是通过把返回的逻辑视图名称去匹配定义好的视图bean对象.它要求视图bean对象都定义在Spring ...

  4. [刘阳Java]_ResourceBundleViewResolver视图解析器_第7讲

    ResourceBundleViewResolver是根据proterties文件来找对应的视图来解析"逻辑视图".该properties文件默认是放在classpath路径下的v ...

  5. [刘阳Java]_SpringMVC访问静态资源_第9讲

    有些时候我们在使用SpringMVC的时候造成无法访问静态资源文件(如:html,js,css,image等等).其主要的原因出在web.xml文件我们设置SpringMVC前端控制器的映射路径 &l ...

  6. [刘阳Java]_InternalResourceViewResolver视图解析器_第6讲

    SpringMVC在处理器方法中通常返回的是逻辑视图,如何定位到真正的页面,就需要通过视图解析器 InternalResourceViewResolver是SpringMVC中比较常用视图解析器. 网 ...

  7. [刘阳Java]_MyBatis_注解基本用法_第10讲

    MyBatis注解提出,可以说是非常好简化了MyBatis配置文件的使用.下面我们简单地来告诉大家如何使用MyBatis的注解 定义接口 package com.gxa.dao; import jav ...

  8. [刘阳Java]_Spring AOP基于XML配置介绍_第9讲

    基于注解配置的Spring AOP固然简单,但是这节我们会给大家介绍基于XML配置的AOP是如何应用的.为什么这么说了,因为后面我们还会介绍到Spring对Dao操作的事务管理(基于AOP的XML文件 ...

  9. [刘阳Java]_Spring AOP注解详细介绍_第8讲

    这节内容非常关键,我们会比较详细地介绍Spring AOP注解的使用 1. 要使用Spring AOP注解,必须满足如下的事项 导入Aspectj的jar.Spring3.0-AOP.jar.aopa ...

随机推荐

  1. JDBC连接MySQL、Oracle和SQL server的配置

    什么是JDBC 我们可以将JDBC看作是一组用于用JAVA操作数据库的API,通过这个API接口,可以连接到数据库,并且使用结构化查询语言(SQL)完成对数据库的查找,更新等操作. JDBC连接的流程 ...

  2. 关于使用JS去除URL中的指定参数问题,js 对url进行某个参数的删除,并返回url

    在网页上找了半天,发现现在的资源实在是少的可怜,而前端尤甚.所以没办法,于是自己花了一些时间写了一个: 1 /** 2 * 删除URL中的指定参数 3 * @param {*} url 4 * @pa ...

  3. 如何基于 String 实现同步锁?

    如何基于String实现同步锁? 在某些时候,我们可能想基于字符串做一些事情,比如:针对同一用户的并发同步操作,使用锁字符串的方式实现比较合理. 因为只有在相同字符串的情况下,并发操作才是不被允许的. ...

  4. 「模拟8.29」chinese(性质)·physics·chemistry(概率期望)

    T1  chinese 根据他的问题i*f[i]我们容易联想到,答案其实是每种方案中每个点的贡献为1的加和 我们可以转变问题,每个点在所有方案的贡献 进而其实询问就是1-k的取值,有多少中方案再取个和 ...

  5. NOIP模拟测试8「寿司」

    考试时打的类似$n^2$暴力,然后炸了只有10分 后来验证我的算法伪了. 题解 显然你有一种解法,假设你要在一个B点断开将R分别移向最左 最右,这样只用分别计算B点右面蓝色数量左面蓝色数量就得到了一个 ...

  6. WEB安全漏洞扫描与处理(上)——安全漏洞扫描工具AppScan的安装使用

    很多公司对软件会有安全的要求,一般测试公司会使用安全漏洞扫描工具对软件进行漏扫,然后给出安全报告,然后软件开发人员会根据提供的安全报告进行漏洞的处理.我们接触到的测评公司,使用的是漏洞扫描工具AppS ...

  7. c#在类中使用session

    先要继承页面的System.Web.UI.Page using System; using System.Collections.Generic; using System.Linq; using S ...

  8. OSPF 路由协议

    OSPF路由协议 目录 一.OSPF路由协议概述 1.1.内部网关和外部网关协议 1.2.OSPF的工作过程 1.3.OSPF的基本概念 二.OSPF 数据包类型 2.1.OSPF数包 2.2.OSP ...

  9. 没有指定非静态方法,Unity与Android通信错误

    报错信息: AndroidJavaException: java.lang.NoSuchMethodError: no non-static method with name='InstallApk' ...

  10. windows下Docker Desktop安装管理

    检查要求 Windows 10 企业版.专业版或教育版 (必须windows10 1903版本以上)版本号 18362.1049+ 或 18363.1049+ ,次版本#大于.1049.最好是最新版( ...