Spring 配置和集成

Posted 小康也想变大佬

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring 配置和集成相关的知识,希望对你有一定的参考价值。

1 创建web工程

使用eclipse创建一个web工程,加入工程修改为支持web3.1,具体的步骤将看到我发表
maven安装配置

2 spring配置

pom文件

<properties>
		<hibernate.version>5.2.12.Final</hibernate.version>
		<mysql.driver.version>5.1.44</mysql.driver.version>
		<spring.version>5.2.6.RELEASE</spring.version>
		<struts2.version>2.5.13</struts2.version>
		<slf4j.version>1.7.7</slf4j.version>
		<log4j.version>2.9.1</log4j.version>
		<disruptor.version>3.2.0</disruptor.version>
		<junit.version>4.12</junit.version>
		<servlet.version>4.0.1</servlet.version>
		<jstl.version>1.2</jstl.version>
		<standard.version>1.1.2</standard.version>
		<tomcat-jsp-api.version>8.5.20</tomcat-jsp-api.version>
	</properties>

	<dependencies>

		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.12</version>
			<scope>test</scope>
		</dependency>

		<!-- spring 相关 -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>$spring.version</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>$spring.version</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-orm</artifactId>
			<version>$spring.version</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>$spring.version</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-test</artifactId>
			<version>$spring.version</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aop</artifactId>
			<version>$spring.version</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aspects</artifactId>
			<version>$spring.version</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-beans</artifactId>
			<version>$spring.version</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jdbc</artifactId>
			<version>$spring.version</version>
		</dependency>

		<!-- hibernate -->
		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-core</artifactId>
			<version>$hibernate.version</version>
		</dependency>
		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-c3p0</artifactId>
			<version>$hibernate.version</version>
		</dependency>
		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-ehcache</artifactId>
			<version>$hibernate.version</version>
		</dependency>

		<!-- 数据库驱动 -->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>$mysql.driver.version</version>
		</dependency>

		<!--3. struts2相关(2.5.13) -->
		<dependency>
			<groupId>org.apache.struts</groupId>
			<artifactId>struts2-core</artifactId>
			<version>$struts2.version</version>
			<exclusions>
				<exclusion>
					<artifactId>commons-io</artifactId>
					<groupId>commons-io</groupId>
				</exclusion>
				<exclusion>
					<artifactId>log4j-api</artifactId>
					<groupId>org.apache.logging.log4j</groupId>
				</exclusion>
			</exclusions>
		</dependency>
		<dependency>
			<groupId>org.apache.struts</groupId>
			<artifactId>struts2-spring-plugin</artifactId>
			<version>$struts2.version</version>
			<exclusions>
				<exclusion>
					<artifactId>spring-web</artifactId>
					<groupId>org.springframework</groupId>
				</exclusion>
				<exclusion>
					<artifactId>spring-beans</artifactId>
					<groupId>org.springframework</groupId>
				</exclusion>
				<exclusion>
					<artifactId>spring-context</artifactId>
					<groupId>org.springframework</groupId>
				</exclusion>
				<exclusion>
					<artifactId>spring-core</artifactId>
					<groupId>org.springframework</groupId>
				</exclusion>
			</exclusions>
		</dependency>

		<!--4. log配置:Log4j2 + Slf4j -->
		<!-- slf4j核心包 -->
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
			<version>$slf4j.version</version>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>jcl-over-slf4j</artifactId>
			<version>$slf4j.version</version>
			<scope>runtime</scope>
		</dependency>

		<!--用于与slf4j保持桥接 -->
		<dependency>
			<groupId>org.apache.logging.log4j</groupId>
			<artifactId>log4j-slf4j-impl</artifactId>
			<version>$log4j.version</version>
			<exclusions>
				<exclusion>
					<artifactId>slf4j-api</artifactId>
					<groupId>org.slf4j</groupId>
				</exclusion>
			</exclusions>
		</dependency>

		<!--核心log4j2jar包 -->
		<dependency>
			<groupId>org.apache.logging.log4j</groupId>
			<artifactId>log4j-api</artifactId>
			<version>$log4j.version</version>
		</dependency>
		<dependency>
			<groupId>org.apache.logging.log4j</groupId>
			<artifactId>log4j-core</artifactId>
			<version>$log4j.version</version>
		</dependency>

		<!--web工程需要包含log4j-web,非web工程不需要 -->
		<dependency>
			<groupId>org.apache.logging.log4j</groupId>
			<artifactId>log4j-web</artifactId>
			<version>$log4j.version</version>
			<scope>runtime</scope>
		</dependency>
		<!--需要使用log4j2的AsyncLogger需要包含disruptor -->
		<dependency>
			<groupId>com.lmax</groupId>
			<artifactId>disruptor</artifactId>
			<version>$disruptor.version</version>
		</dependency>

		<!--5. other -->
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>$junit.version</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
			<version>$servlet.version</version>
			<scope>provided</scope>
		</dependency>

		<!--6. jstl -->
		<dependency>
			<groupId>javax.servlet.jsp.jstl</groupId>
			<artifactId>jstl</artifactId>
			<version>$jstl.version</version>
		</dependency>
		<dependency>
			<groupId>taglibs</groupId>
			<artifactId>standard</artifactId>
			<version>$standard.version</version>
		</dependency>

		<!-- 7. jsp自定义标签依赖 (必须与tomcat的版本一致) -->
		<dependency>
			<groupId>org.apache.tomcat</groupId>
			<artifactId>tomcat-jsp-api</artifactId>
			<version>$tomcat-jsp-api.version</version>
			<scope>provided</scope>
		</dependency>

	</dependencies>

注: 该依赖配置不仅包含了spring,同时也包含了hibernate,struts,mysql驱动等。

spring配置文件

spring-bean.xml

<?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:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">


	
</beans>
<bean id="demo" class="org.lisen.springdemo.model.Demo">
		<property name="name">
			<value>张三</value>
		</property>
	</bean>

配置文件说明:
属性 :

  • id:在容器中查找Bean的id(唯一、且不能以/开头)

  • class:bean的完整类名

  • name:在容器中查找Bean的名字(唯一、允许以/开头、允许多个值,多个值之间用逗号或空格隔开)

  • scope:(singleton|prototype)默认是singleton
    singleton: 单例模式, 在每个Spring IoC容器中一个bean定义对应一个对象实例
    prototype: 原型模式/多例模式, 一个bean定义对应多个对象实例

  • parent:指定一个父bean(必须要有继承关系才行)

  • abstract: 将一个bean定义成抽象bean(抽象bean是不能实例化的),抽象类一定要定义成抽象bean,非抽象类也可以定义成抽象bean

  • init-method=“init” 初始化调用对象的init方法

  • ref 引用

    标签:

    属性的配置

	//<bean></bean>是javaBean 
	//<property></<property> 找属性名赋值
	//<value>值</value>
	<bean id="test" class="实体类全限类名">
		<property name="name">
			<value>小康</value>
		</property>
		
		
		//<constructor-arg></<constructor-arg>找有参数构造方法赋值
		<constructor-arg name="name">
			<value>小康</value>
		</constructor-arg>
	</bean>

复杂属性的配置

//复杂属性的配置
	
	<bean id="test" class="实体类全限类名">
		<property name="name">
			<value>小康</value>
		</property>
		//ref 引用
		<property name="addr" ref="addr"/>
	<

以上是关于Spring 配置和集成的主要内容,如果未能解决你的问题,请参考以下文章

Spring 配置和集成

Spring 配置和集成

使用 spring 集成自动配置 ActiveMQ

Spring Data 配置和 Postgres 数据库集成(无 xml 配置)

带有配置文件的 Spring 集成测试

spring配置Bean