Spring 配置基础

1:依赖注入 IOC 配置

1)Java 代码

package com.ceshi.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import com.ceshi.interfaces.IServiceFactory;
import com.ceshi.interfaces.IServices;

public class ServiceFactorys implements IServiceFactory {
	private Properties properties = new Properties();
	private List<String> list = new ArrayList<String>();
	private Map<String, String> map = new HashMap<String, String>();
	private String str;
	private IServices iService;
	
	public ServiceFactorys() {
		System.out.println("ServiceFactorys - 被加载");
	}
	
	public Properties getProperties() {
		return properties;
	}
	public void setProperties(Properties properties) {
		this.properties = properties;
	}
	public List<String> getList() {
		return list;
	}
	public void setList(List<String> list) {
		this.list = list;
	}
	public Map<String, String> getMap() {
		return map;
	}
	public void setMap(Map<String, String> map) {
		this.map = map;
	}
	public String getStr() {
		return str;
	}
	public void setStr(String str) {
		this.str = str;
	}
	public IServices getiService() {
		return iService;
	}
	public void setiService(IServices iService) {
		this.iService = iService;
	}


	/* (non-Javadoc)
	 * @see com.ceshi.service.IServiceFactory#add()
	 */
	@Override
	public void add() {
		System.out.println("THIS IS ServiceFactorys CLASS - add()");
		iService.save();
		System.out.println(this.getStr());
	}
}
2)XML 配置

<?xml version="1.0" encoding="UTF-8" standalone="no"?>

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	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-2.5.xsd
						
						http://www.springframework.org/schema/context
						http://www.springframework.org/schema/context/spring-context-2.5.xsd
						
						http://www.springframework.org/schema/tx
						http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
	<!-- 
		beans 配置
		default-lazy-init="true":所有Bean延迟初始化
	 -->
<!-- =========================================================================== -->

	<!-- 
		bean配置
		id:唯一  XML的一个属性  不能有特殊字符
		name:可以有特殊字符
		class:实例化的类
		scope:singleton(单例)/prototype(多实例)
		lazy-init:true(延迟加载)/false(other)
		init-method:首先被加载
		destroy-method:最后被执行
	 -->
	 
	<bean id="services" class="com.ceshi.service.Services"/>
	
	<bean id="serviceFactory" class="com.ceshi.service.ServiceFactorys">
		<!-- 构造方法注入 -->
		<!-- 
		<constructor-arg index="第一个 0" type="对象.class" ref="依赖对象" value="基本类型值" />
		<constructor-arg index="第二个 1" type="对象.class" ref="依赖对象" value="基本类型值" />
		 -->
		 
		<!-- 注入类 -->
		<property name="iService" ref="services" />
		
		<!-- 注入普通数据类型属性值 -->
		<property name="str" value="CESHI-STR" />
		
		<!-- 注入集合 -->
		<property name="list">
			<list>
				<value>第一个</value>
				<value>第二个</value>
				<value>第三个</value>
			</list>
		</property>
		<property name="map">
			<map>
				<entry key="map0" value="map0"></entry>
				<entry key="map1" value="map1"></entry>
				<entry key="map2" value="map2"></entry>
			</map>
		</property>
		
		<!-- 注入properties -->
		<property name="properties">
			<props>
				<prop key="prop0">prop0</prop>
				<prop key="prop1">prop1</prop>
				<prop key="prop2">prop2</prop>
			</props>
		</property>
	
	</bean>

</beans>

2:切面编程AOP 配置

1)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:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop"
	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
		http://www.springframework.org/schema/aop
		http://www.springframework.org/schema/aop/spring-aop.xsd">

	<!-- 开启SpringAOP模式 调用解析器 -->
	<aop:aspectj-autoproxy />
	
	<bean id="loginBean" class="com.springaop.bean.LoginBean" />
	
	<!-- 注解实现AOP
	<bean id="asProxy" class="com.springaop.bean.AsProxy"></bean>
	 -->
	
	<!-- 在配置文件中对切面进行配置 -->
	<bean id="asProxyM" class="com.springaop.bean.AsProxyM"></bean>
	<aop:config>
		<aop:aspect id="myaop" ref="asProxyM">
			<!-- 声明切面 -->
			<aop:pointcut expression="execution (* com.springaop.bean.LoginBean.*(..))" id="myaopa" />
			<aop:before method="before" pointcut-ref="myaopa" />
			<aop:after-returning method="afterReturning" pointcut-ref="myaopa" />
			<aop:after-throwing method="afterThrowing" pointcut-ref="myaopa" />
			<aop:after method="after" pointcut-ref="myaopa" />
			<aop:around method="around" pointcut-ref="myaopa" />
		</aop:aspect>
	</aop:config>	
	
</beans>
2)注解代码中配置

package com.springaop.bean;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

/**
 * 切面类
 */
@Aspect
public class AsProxy {
	/**
	 * 声明一个切入点
	 * ("execution (* com.springaop..*.*(..))")
	 * ("execution (所有返回值类型 包下子包所有类 所有方法(方法任意参数))")
	 */
	@Pointcut("execution (* com.springaop.bean.LoginBean.*(..))")
	private void anyMethod() {}
	
	/**
	 * 前置通知  (value = "anyMethod()")
	 * 再通知中设置形参
	 */
	@Before(value = "anyMethod() && args(str)")
	public void before(String str) {
		System.out.println("THIS IS ASPROXY CLASS - befor():" + str);
	}
	
	/**
	 * 后置通知  结果返回增强处理
	 * 通知中设置返回值 显示的是返回的值
	 */
	@AfterReturning(pointcut = "anyMethod()", returning = "str")
	public void afterReturning(String str) {
		System.out.println("THIS IS ASPROXY CLASS - afterReturning():" + str);
	}
	
	//最终通知
	@After(value = "anyMethod()")
	public void after() {
		System.out.println("THIS IS ASPROXY CLASS - after()");
	}
	
	/**
	 * 异常通知  如果抛出异常就不会执行后置通知
	 * 得到抛出的异常
	 */
	@AfterThrowing(pointcut = "anyMethod()", throwing = "e")
	public void afterThrowing(Exception e) {
		System.out.println("THIS IS ASPROXY CLASS - afterThrowing():" + e);
	}
	
	/**
	 * 环绕通知
	 * 权限判断
	 */
	@Around(value = "anyMethod()")
	public Object around(ProceedingJoinPoint procee) throws Throwable {
		System.out.println("THIS IS ASPROXY CLASS - around(ProceedingJoinPoint in)");
		//必须执行方法 不调用后边的切面方法将不执行
		Object object = procee.proceed();
		System.out.println("THIS IS ASPROXY CLASS - around(ProceedingJoinPoint out)");
		return object;
	}
}

3:常用声明注解

1)声明

/**
	 * 注解注入
	 * @Autowired :
	 * 		按类型装配
	 * @Resource ://不是Spring中的
	 * 		按名称装配 找不到匹配再按类型 = 有name属性
	 * 		可以用在属性的定义 方法的定义=一定是set方法
	 * ===========================================
	 * ===========================================
	 * Bean配置注解
	 * @Component(类注解) :
	 * 		组件(任意类型)
	 * @Service(类注解)
	 * 		逻辑层
	 * @Controller(类注解)
	 * 		控制层 /Struts的Action类上边
	 * @Repository(类注解)
	 * 		数据访问组件DAO组件
	 * @Scope
	 * 		修改Bean的作用域
	 * @PostConstruct : //不是Spring注解 ejb3中的注解 被广泛应用
	 * 		声明init()方法实例化后直接被执行
	 * @PreDestroy : //不是Spring注解 ejb3中的注解 被广泛应用
	 * 		声明destroy()摧毁实例化后被执行
	 * 
	 * ===========================================
	 * ===========================================
	 * 
	 * 
	 */

2)XML 配置

<?xml version="1.0" encoding="UTF-8" standalone="no"?>

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	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-2.5.xsd
						
						http://www.springframework.org/schema/context
						http://www.springframework.org/schema/context/spring-context-2.5.xsd
						
						http://www.springframework.org/schema/tx
						http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
	
	
	<!-- 声明使用注解
		隐式声明注册对注解进行解析的解析处理器
	 -->
	<context:annotation-config />
	
	<!-- 自动装配Bean
		注册了很多解析处理器
	 -->
	<context:component-scan base-package="cn.test"></context:component-scan>
	
	<!--
	<bean id="serviceFactory" class="cn.test.services.ServiceFactory" />
	<bean id="iServices" class="cn.test.services.Services" />
	 -->

</beans>

4:cgLib 代理工厂

package com.cglibaop;

import java.lang.reflect.Method;

import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

public class Factopy implements MethodInterceptor {
	private Object object;
	public Object intenceFactory(Object object) {
		this.object = object;
		//创建代理对象
		Enhancer enhancer = new Enhancer();
		//创建父类目标类 覆写所有非final修饰方法属性
		enhancer.setSuperclass(this.object.getClass());
		//代理回调方法
		enhancer.setCallback(this);
		//返回代理对象
		return enhancer.create();
	}
	
	@Override
	public Object intercept(
				Object object,//代理对象本身
				Method method,//拦截到的方法
				Object[] args,//方法参数
				MethodProxy methodProxy)//
						throws Throwable {
		Object obj = methodProxy.invoke(object, args);
		return obj;
	}
	
}
-- END

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值