现在的位置: 首页 > 综合 > 正文

spring 注解配置

2018年03月22日 ⁄ 综合 ⁄ 共 3268字 ⁄ 字号 评论关闭

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

http://www.springframework.org/schema/beans


http://www.springframework.org/schema/beans/spring-beans-3.0.xsd


http://www.springframework.org/schema/context


http://www.springframework.org/schema/context/spring-context-3.0.xsd


http://www.springframework.org/schema/mvc


http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd


http://www.springframework.org/schema/tx


http://www.springframework.org/schema/tx/spring-tx-3.0.xsd


http://www.springframework.org/schema/aop

        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
        
        
 
	<bean id="placeholderConfig"
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
		<list>
			<value>classpath:init.properties</value>
		</list>
		</property>
	</bean>
	 <!-- ①:对所有包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能 
	       使用 annotation 自动注册bean,并检查@Controller, @Service, @Repository注解已被注入 -->
	<context:component-scan base-package="*" />

	<!-- ②:启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
	<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
	</bean>
	<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
	<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
		<constructor-arg ref="dataSource" />
	</bean>
	<bean id="parent_dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close" >
		<property name="acquireIncrement">
			<value>${c3p0.acquireIncrement}</value>
		</property>
		<property name="initialPoolSize">
			<value>${c3p0.initialPoolSize}</value>
		</property>
		<property name="minPoolSize">
			<value>${c3p0.minPoolSize}</value>
		</property>
		<property name="maxPoolSize">
			<value>${c3p0.maxPoolSize}</value>
		</property>
		<property name="maxIdleTime">
			<value>${c3p0.maxIdleTime}</value>
		</property>
		<property name="idleConnectionTestPeriod">
			<value>${c3p0.idleConnectionTestPeriod}</value>
		</property>
		<property name="maxStatements">
			<value>${c3p0.maxStatements}</value>
		</property>
		<property name="numHelperThreads">
			<value>${c3p0.numHelperThreads}</value>
		</property>
	</bean>
	
	<bean id="dataSource" parent="parent_dataSource">
		<property name="driverClass">
			<value>${ecp_datasource.driverClassName}</value>
		</property>
		<property name="jdbcUrl">
			<value>${ecp_datasource.url}</value>
		</property>
		<property name="user">
			<value>${ecp_datasource.username}</value>
		</property>
		<property name="password">
			<value>${ecp_datasource.password}</value>
		</property>
	</bean>
</beans>

二、 基础DaoImpl实现类需要加的注解

public class DaoImpl {

    @Resource
    private JdbcTemplate jdbcTemplate;

    /***下面是你的的sql基本操作方法**/

}

三、Action需要加的注解

@Controller
public class AliPayAction extends BaseAction{

    @Resource
    private IndexService        indexService;//你的业务接口

     /***下面是你的的action基本操作方法**/

}

四、接口的实现类需要加的注解

@Service("indexService")
public class IndexServiceImpl implements IndexService {
    @Resource
    private IndexDao indexDao;

/***下面是你的的业务实现方法**/

}

五、dao实现类的注入需要加的注解

@Service("indexDao")
public class IndexDaoImpl extends DaoImpl implements IndexDao {

   /***下面是你的的业务实现方法**/

 

}

 

抱歉!评论已关闭.