Java(框架)Spring(一)

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

package com.openlab.soldierboy;

public class SoldierBoy {
	
	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 soldierBoy(){
		System.out.println(who1+"说:三天不打小鬼子,手都痒痒!");
		System.out.println(who2+"说:世界上有十种人,认识二进制的和不认识二进制的。");
	}
}

applicationContext.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"
	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 SoldierBoy -->
	<bean id="soldierboy" class="com.openlab.soldierboy.SoldierBoy">
		<!-- 注入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.soldierboy.SoldierBoy;

public class Test01 {
	
	@Test
	public void TestSoldierBoy(){
		ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
	
		//控制翻转
		SoldierBoy soldierBoy = (SoldierBoy)applicationContext.getBean("soldierboy");
		soldierBoy.soldierBoy();
	
	
	}
}

实现效果:

在这里插入图片描述需求:

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

package com.openlab.paper;

public interface Paper {

		public String getSize();
}

A1Paper:

package com.openlab.paper.impl;

import com.openlab.paper.Paper;

public class A1Paper implements Paper {

	@Override
	public String getSize() {
		return "A1纸张";
	}
}

A2Paper:

package com.openlab.paper.impl;

import com.openlab.paper.Paper;

public class A2Paper implements Paper {

	@Override
	public String getSize() {
		return "A2纸张";
	}
}

Ink接口:

package com.openlab.ink;

public interface Ink {
	
	public String getColor();
}

实现类:

package com.openlab.ink.impl;

import com.openlab.ink.Ink;

public class GreenInkImpl implements Ink{
	
	@Override
	public String getColor(){
		return "绿色墨盒";
	}
}
package com.openlab.ink.impl;

import com.openlab.ink.Ink;

public class YellowInkImpl implements Ink {
	
	@Override
	public String getColor(){
		return "黄色墨盒";
	}
}

applicationContext.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"
	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 SoldierBoy -->
	<bean id="soldierboy" class="com.openlab.soldierboy.SoldierBoy">
		<!-- 注入who属性 -->
		<property name="who1" value="张嘎"></property>
		<property name="who2" value="Rod"></property>
	</bean>
	
	<!-- 配置A1纸张 -->
	<bean id="A1" class="com.openlab.paper.impl.A1Paper"></bean>
	<!-- 配置A2纸张 -->
	<bean id="A2" class="com.openlab.paper.impl.A2Paper"></bean>
	
	<!-- 配置黄色墨盒 -->
	<bean id="yellow" class="com.openlab.ink.impl.YellowInkImpl"></bean>
	<!-- 配置绿色墨盒 -->
	<bean id="green" class="com.openlab.ink.impl.GreenInkImpl"></bean>
	
	<!-- 组装打印机 -->
	<bean id="printer" class="com.openlab.printer.Printer">
	<!-- 注入墨盒 -->
	<property name="ink" ref="yellow"></property>
	<!-- 注入纸张 -->
	<property name="paper" ref="A1"></property>
	</bean>
</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: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 SoldierBoy -->
	<bean id="soldierboy" class="com.openlab.soldierboy.SoldierBoy">
		<!-- 注入who属性 -->
		<property name="who1" value="张嘎"></property>
		<property name="who2" value="Rod"></property>
	</bean>
	
	<!-- 配置A1纸张 -->
	<bean id="A1" class="com.openlab.paper.impl.A1Paper"></bean>
	<!-- 配置A2纸张 -->
	<bean id="A2" class="com.openlab.paper.impl.A2Paper"></bean>
	
	<!-- 配置黄色墨盒 -->
	<bean id="yellow" class="com.openlab.ink.impl.YellowInkImpl"></bean>
	<!-- 配置绿色墨盒 -->
	<bean id="green" class="com.openlab.ink.impl.GreenInkImpl"></bean>
	
	<!-- 组装打印机 -->
	<bean id="printer" class="com.openlab.printer.Printer">
	<!-- 注入墨盒 -->
	<property name="ink" ref="yellow"></property>
	<!-- 注入纸张 -->
	<property name="paper" ref="A1"></property>
	</bean>
</beans>

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

package com.openlab.dao;

import java.util.List;

import com.openlab.pojo.Users;

public interface UserDao {

	public int addUser(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 addUser(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("用户删除开始!");
		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.dao.UserDao;
import com.openlab.pojo.Users;
import com.openlab.service.UsersService;

public class UsersServiceImpl implements UsersService {
	
	private UserDao userDao;

	public UsersServiceImpl() {
		
	}

	public UsersServiceImpl(UserDao userDao) {
		this.userDao = userDao;
	}

	public UserDao getUserDao() {
		return userDao;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}
	
	@Override
	public int addNewUserService(Users _user) {
		return userDao.addUser(_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);
	}

}
<?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 -->
	<bean id="userDao" class="com.openlab.dao.impl.UserDaoImpl"></bean>

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

	<!-- 配置增强处理类的Bean -->
	<bean id="usersAspect" class="com.openlab.aspect.UsersAspect"></bean>
	<!-- AOP配置 -->
	<aop:config>
		<!-- 配置切入点 -->
		<aop:pointcut expression="execution(public * addNewUserService(..))"
			id="pointCut1" />
		<aop:pointcut expression="execution(public * modifyUserService(..))"
			id="pointCut2" />
		<!-- 配置切面 -->
		<aop:aspect ref="usersAspect">
			<!-- 前置增强 -->
			<aop:before method="beforeAspectMethod" pointcut-ref="pointCut2" />
			<!-- 后置处理 -->
			<aop:after-returning method="afterReturningMethod"
				pointcut-ref="pointCut2" returning="result" />
		</aop:aspect>
	</aop:config>
</beans>

测试类:

package com.openlab.test;

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

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

public class Test03 {

	@Test
	public void test03(){
		ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
		
		//获取业务逻辑层Bean
		UsersService userService=(UsersService)applicationContext.getBean("usersService");
		
		Users _user=new Users();
		_user.setId(001);
		_user.setName("武大郎");
		_user.setAddress("山东");
		
		//userService.addNewUserService(_user);
		userService.modifyUserService(001, _user);
		
	}
	
}

实现效果:
在这里插入图片描述
以上均为个人所写,如有错误,欢迎指正,谢谢~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值