Spring(一)

需求:
在这里插入图片描述
代码实现:

package com.openlab.whosay;

public class whoSay {
	private String who1="";
	private String who2="";
	
	public String getWho1() {
		return who1;
	}
	public void setWho1(String who1) {
		this.who1 = who1;
	}
	public String getWho2() {
		return who2;
	}
	public void setWho2(String who2) {
		this.who2 = who2;
	}
	
	public void whoSay(){
		System.out.println(who1+"说:三天不打鬼子,手都痒痒!");
		System.out.println(who2+"说:世界上有十种人,认识二进制和不认识二进制的。");
	}
}
<?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"
	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">

	<!--配置bean whoSay -->
	<bean id="whosay" class="com.openlab.whosay.whoSay">
		<!-- 注入who属性 -->
		<property name="who1" value="张嘎"></property>
		<property name="who2" value="Rod"></property>
	</bean>
</beans>
package com.openlab.test;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.openlab.whosay.whoSay;

public class test01 {
	
	@Test
	public void testWhoSay(){
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
		
		//控制翻转
		whoSay whosay = (whoSay)applicationContext.getBean("whosay");
		whosay.whoSay();
	}
}

效果截图:
在这里插入图片描述
需求:
在这里插入图片描述
代码实现:

package com.openlab.ink;

public interface Ink {
	
	public String getColor();
}
package com.openlab.ink.impl;

import com.openlab.ink.Ink;

public class blackInkImpl implements Ink{

	@Override
	public String getColor() {
		return "黑色墨盒";
	}	
}
package com.openlab.ink.impl;

import com.openlab.ink.Ink;

public class redInkImpl implements Ink{

	@Override
	public String getColor() {
		return "红色墨盒";
	}
}
package com.openlab.paper;

public interface Paper {
	public String getSize();
}
package com.openlab.paper.impl;

import com.openlab.paper.Paper;

public class A4Paper implements Paper {

	@Override
	public String getSize() {
		return "A4纸张";
	}
}
package com.openlab.paper.impl;

import com.openlab.paper.Paper;

public class B2Paper implements Paper{

	@Override
	public String getSize() {
		return "B2纸张";
	}
}
package com.openlab.printer;

import com.openlab.ink.Ink;
import com.openlab.paper.Paper;

public class Printer {
	private Ink ink;
	private Paper paper;
	
	
	public Printer() {
	}
	
	public Printer(Ink ink, Paper paper) {
		super();
		this.ink = ink;
		this.paper = paper;
	}

	public Ink getInk() {
		return ink;
	}
	public void setInk(Ink ink) {
		this.ink = ink;
	}
	public Paper getPaper() {
		return paper;
	}
	public void setPaper(Paper paper) {
		this.paper = paper;
	}
	
	public void printer(){
		System.out.println("打印机使用"+ink.getColor()+"在"+paper.getSize()+"上开始打印。。。");
	}
}
<?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"
	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">

	<!--配置bean whoSay -->
	<bean id="whosay" class="com.openlab.whosay.whoSay">
		<!-- 注入who属性 -->
		<property name="who1" value="张嘎"></property>
		<property name="who2" value="Rod"></property>
	</bean>

	<!-- 配置A4纸 -->
	<bean id="A4" class="com.openlab.paper.impl.A4Paper"></bean>
	<!-- 配置B2纸 -->
	<bean id="B2" class="com.openlab.paper.impl.B2Paper"></bean>

	<!-- 配置黑色墨盒 -->
	<bean id="black" class="com.openlab.ink.impl.blackInkImpl"></bean>
	<!-- 配置红色墨盒 -->
	<bean id="red" class="com.openlab.ink.impl.redInkImpl"></bean>

	<!-- 组装打印机 -->
	<bean id="printer" class="com.openlab.printer.Printer">
		<!-- 注入墨盒 -->
		<property name="ink" ref="black"></property>
		<!-- 注入纸张 -->
		<property name="paper" ref="B2"></property>

	</bean>
</beans>
package com.openlab.test;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.openlab.printer.Printer;

public class test02 {
	
	@Test
	public void testPrinter(){
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
		
		Printer printer = (Printer)applicationContext.getBean("printer");
		printer.printer();
	}
}

效果截图:
在这里插入图片描述
需求:
在这里插入图片描述
代码实现:

package com.openlab.aspect;

import java.util.Arrays;

import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;

public class UsersAspect {
	
	private static final Logger log=Logger.getLogger(UsersAspect.class);
	
	//声明前置增强处理
	
	public void beforeAspectMethod(JoinPoint jp){
		log.info("使用"+jp.getSignature().getName()+"进行增强处理,方法的参数:"+Arrays.toString(jp.getArgs()));
		
	
	}
	
	//声明后置增强处理
	public void afterReturningMethod(JoinPoint jp,Object result){
		log.info("使用"+jp.getSignature().getName()+"进行后置增强,方法的参数列表"+Arrays.toString(jp.getArgs())+"返回值:"+result);
		
	}
}
package com.openlab.dao;

import java.util.List;

import com.openlab.pojo.Users;

public interface UserDao {
	
	public int addnewUser(Users _user);//用户添加
	
	public int modifyUser(int _id,Users user);//用户修改
	
	public List<Users> findAll();//显示所有用户
	
	public int deleteUserById(int _id);//删除用户
	
}

package com.openlab.dao.impl;

import java.util.List;

import com.openlab.dao.UserDao;
import com.openlab.pojo.Users;

public class UserDaoImpl implements UserDao{

	@Override
	public int addnewUser(Users _user) {
		System.out.println("用户添加开始。。。");
		return 0;
	}

	@Override
	public int modifyUser(int _id, Users user) {
		System.out.println("用户修改开始。。。");

		return 0;
	}

	@Override
	public List<Users> findAll() {
		System.out.println("查询所有用户。。。");

		return null;
	}

	@Override
	public int deleteUserById(int _id) {
		System.out.println("删除指定id的用户。。。");

		return 0;
	}
}
package com.openlab.pojo;

public class Users {
	private int id;
	private String name;
	private String address;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
}
package com.openlab.Service;

import java.util.List;

import com.openlab.pojo.Users;

public interface UsersService {
	public int addnewUserService(Users _user);//用户添加
	
	public int modifyUserService(int _id,Users user);//用户修改
	
	public List<Users> findAllService();//显示所有用户
	
	public int deleteUserByIdService(int _id);//删除用户
}
package com.openlab.Service.impl;

import java.util.List;

import com.openlab.Service.UsersService;
import com.openlab.dao.UserDao;
import com.openlab.pojo.Users;

public class UserServiceImpl implements UsersService {

	private UserDao userDao;//面向接口编程,依赖注入
	
	
		
	public UserDao getUserDao() {
		return userDao;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	@Override
	public int addnewUserService(Users _user) {
		//控制数据库连接
		//处理事务
		//增加业务逻辑
		return userDao.addnewUser(_user);
	}

	@Override
	public int modifyUserService(int _id, Users user) {
		return userDao.modifyUser(_id, user);
	}

	@Override
	public List<Users> findAllService() {
		return userDao.findAll();
	}

	@Override
	public int deleteUserByIdService(int _id) {
		return userDao.deleteUserById(_id);
	}
}
package com.openlab.test;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.openlab.Service.UsersService;
import com.openlab.pojo.Users;

public class test03 {
	
	@Test
	public void test03(){
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
		
		//获取业务逻辑层Bean
		UsersService userService = (UsersService)applicationContext.getBean("userService");
		
		Users _user = new Users();
		_user.setId(001);
		_user.setName("武松");
		_user.setAddress("山东");
		
		userService.addnewUserService(_user);
	}
}
<?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"
	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">

	<!--配置bean whoSay -->
	<bean id="whosay" class="com.openlab.whosay.whoSay">
		<!-- 注入who属性 -->
		<property name="who1" value="张嘎"></property>
		<property name="who2" value="Rod"></property>
	</bean>

	<!-- 配置A4纸 -->
	<bean id="A4" class="com.openlab.paper.impl.A4Paper"></bean>
	<!-- 配置B2纸 -->
	<bean id="B2" class="com.openlab.paper.impl.B2Paper"></bean>

	<!-- 配置黑色墨盒 -->
	<bean id="black" class="com.openlab.ink.impl.blackInkImpl"></bean>
	<!-- 配置红色墨盒 -->
	<bean id="red" class="com.openlab.ink.impl.redInkImpl"></bean>

	<!-- 组装打印机 -->
	<bean id="printer" class="com.openlab.printer.Printer">
		<!-- 注入墨盒 -->
		<property name="ink" ref="black"></property>
		<!-- 注入纸张 -->
		<property name="paper" ref="B2"></property>

	</bean>

	<!-- 配置数据访问层Bean -->
	<bean id="userDao" class="com.openlab.dao.impl.UserDaoImpl"></bean>

	<!-- 配置业务逻辑层Bean -->
	<bean id="userService" class="com.openlab.Service.impl.UserServiceImpl">
		<!-- 依赖注入数据组件 -->
		<property name="userDao" ref="userDao"></property>
	</bean>

	<!-- 配置增强处理类的Bean -->
	<bean id="userAspect" class="com.openlab.aspect.UsersAspect"></bean>

	<!-- AOP配置开始 -->
	<aop:config>
		<!-- 配置切入点 -->
		<aop:pointcut
			expression="execution(public int addnewUserService(com.openlab.pojo.Users))"
			id="pointCut1" />

		<!-- 配置切面 -->
		<aop:aspect ref="userAspect">
			<!-- 配置增强处理方式,前置增强 -->
			<aop:before method="beforeAspectMethod" pointcut-ref="pointCut1" />

			<!-- 配置增强处理方式,法后置增强 -->
			<aop:after-returning method="afterReturningMethod"
				pointcut-ref="pointCut1" returning="result" />

		</aop:aspect>
		<aop:aspect ref="errorsAspect">
			<aop:after-throwing method="ErrorsMethod"
				pointcut-ref="pointCut2" throwing="e" />
		</aop:aspect>
	</aop:config>

</beans>

效果截图:
在这里插入图片描述
如有错误请大家指正。


版权声明:本文为weixin_44502548原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。