Spring框架配置拓展

1,数据库配置–配置数据源

​ 使用properties文件引入,定制数据源时,引入配置文件中的属性

<!--	引入properties文件-->
	<bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
		<property name="location" value="classpath:db.properties"/>
	</bean>

	<!--	数据源-->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName" value="${driverClassName}"/>
		<property name="url" value="${url}"/>
		<property name="username" value="${uname}"/>
		<property name="password" value="${password}"/>
	</bean>

​ properties文件中



driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/smbms?useUnicode=true&amp;characterEncoding=UTF-8
password=root
uname=root

2,JNDI(javaNameAndDirectoryInterface)配置数据源

在Tomcat中conf目录下的context.xml文件中,配置如下

<!-- Global JNDI resources
       Documentation at /docs/jndi-resources-howto.html
  -->
  <GlobalNamingResources>
    <!-- Editable user database that can also be used by
         UserDatabaseRealm to authenticate users
    -->
    <Resource name="UserDatabase" auth="Container"
              type="org.apache.catalina.UserDatabase"
              description="User database that can be updated and saved"
              factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
              pathname="conf/tomcat-users.xml" />
<!--
  |- name:表示以后要查找的名称。通过此名称可以找到DataSource,此名称任意更换,但是程序中最终要查找的就是此名称,
           为了不与其他的名称混淆,所以使用jdbc/oracle,现在配置的是一个jdbc的关于oracle的命名服务。
  |- auth:由容器进行授权及管理,指的用户名和密码是否可以在容器上生效
  |- type:此名称所代表的类型,现在为javax.sql.DataSource
  |- maxActive:表示一个数据库在此服务器上所能打开的最大连接数
  |- maxIdle:表示一个数据库在此服务器上维持的最小连接数
  |- maxWait:最大等待时间。10000毫秒
  |- username:数据库连接的用户名
  |- password:数据库连接的密码
  |- driverClassName:数据库连接的驱动程序
  |- url:数据库连接的地址
-->

<!--配置MySQL数据库的JNDI数据源-->
<Resource
        name="jdbc/mysql"
        auth="Container"
        type="javax.sql.DataSource"
        maxActive="100"
        maxIdle="30"
        maxWait="10000"
        username="root"
        password="root"
        driverClassName="com.mysql.jdbc.Driver"
        url="jdbc:mysql://192.168.1.144:3306/leadtest?useUnicode=true&characterEncoding=utf-8"/>

  </GlobalNamingResources>

在Spring核心配置文件中编写如下代码:

<!--	通过JNDI配置DataSource-->
	<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
        <!-- name的值时固定的,value的值java:comp/env/此部分是固定的,后面的部分是JNDI数据源中的name  -->
		<property name="jndiName" value="java:comp/env/jdbc/mysql"/>
	</bean>
</beans>

3,拆分Spring配置文件—2种方式

1>把Spring配置文件按照一定规则拆分成若干个文件,通过ClassPathXmlApplicationContext类提供的重载进行合成加载。

在这里插入图片描述

如果有多个配置文件需要载入,可以用多字符串参数或String[]数组形式传入多个配置文件名,也可通过通配符的形式进行传参;

//1--使用通配符的方法
        ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext*.xml");

//2--用多字符串参数
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml","applicationContext-dao.xml","applicationContext-service.xml");

//3--使用String[]数组形式
 String [] configs={"applicationContext.xml","applicationContext-dao.xml","applicationContext-service.xml"};
ApplicationContext context=new ClassPathXmlApplicationContext(configs);
2>通过import标签,在Spring核心配置文件中载入其他部分的配置文件,加载时,只需指定核心配置文件即可。
	<import resource="applicationContext-dao.xml"/>
	<import resource="applicationContext-service.xml"/>

Bean的自动装配

1,在xml配置文件中使用bean标签的autowire属性

​ 其属性值:

说明
no不使用自动装配。必须通过property元素定义
byName根据属性明自动装配。BeanFactory查找容器中的全部Bean,找到id与属性的setter方法匹配的Bean,找到则自动注入,否则什么都不做。
byType根据属性类型自动装配,BeanFactory查找容器中的全部Bean,如果正好有一个依赖属性类型相同的Bean,则自动装配这个属性;如果有多个这样的bean,则会抛出异常;如果没有匹配的Bean,则什么都不会发生,属性不会被设置。
constructor应用于构造器参数,与byType相似,如果容器中没有与之构造器参数类型一直的bean,则会抛出异常。
2,配置整个配置文件中的beans根节点统一配置
<?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:aop="http://www.springframework.org/schema/aop" 
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.2.xsd" 	default-autowire="byName">
</beans>

bean的作用域(声明周期)

其属性值有:

属性说明
singleton默认值,以单例模式创建bean的实例。
prototype每次从容其中获取bean时,都会创建一个新的实例。
1> 使用时:在bean标签中的scope属性中指定
<!--Eg:-->
<bean class="com.demo.pojo.User" id="user" autowire="byType" scope="singleton">
		<property name="userName" value="小明"/>
		<property name="id" value="2"/>
	</bean>

2> 使用时:在在类上加上相应注解
/**
*此时,这个类在使用时,只能被创建一次
*/
@Scope("singleton")
@Service
public class ProviderServiceImpl implements ProviderService {
    @Autowired
    private ProviderMapper providerMapper;

    @Override
    public int addinfo(Provider provider) {
        return providerMapper.addinfo(provider);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Spring是一个开源的轻量级JavaEE应用框架,由于它的便捷性、高效性、可控性和可维护性,已经成为java开发的事实上的标准。Spring提供了一系列的核心模块,包括IoC容器、AOP、数据访问、Web开发、消息队列等,以及众多的拓展模块,如Spring Security、Spring Batch等等。Spring框架最早由Rod Johnson在2002年创建,目前由Pivotal公司进行维护和更新。 Spring框架的核心理念是“控制反转(IoC)”和“面向切面编程(AOP)”。其中,控制反转是指通过配置文件或注解等方式将对象的创建和依赖注入的控制权从程序员手中转移给框架,从而实现了松耦合和可维护性;面向切面编程则是通过在不改变原有业务逻辑的情况下,将与业务逻辑无关的横切逻辑(如日志、事务、安全等)从业务逻辑中分离出来,提高了代码的复用性和可维护性。 Spring框架的优点包括: 1. 易于学习和使用,提高开发效率。 2. 松耦合,降低代码的复杂度和维护成本。 3. 支持AOP编程,提高代码的复用性和可维护性。 4. 提供了丰富的第三方拓展模块,如Spring Security、Spring Batch等。 5. 支持多种数据访问方式,如JDBC、Hibernate、MyBatis等。 6. 提供了完善的测试框架,如JUnit、Mockito等。 7. 支持RESTful架构,适合构建Web服务。 8. 可扩展性强,支持自定义注解、拦截器等。 总之,Spring框架是一个功能强大、灵活性高、易于扩展、易于维护的JavaEE应用框架

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值