0714框架课程作业
一、作业1
1.创建项目,并导入spring相关的jar包
2.创建一个People类
package com.openlab.pojo;
public class People {
private String name;
private String words;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getWords() {
return words;
}
public void setWords(String words) {
this.words = words;
}
public void talk(){
System.out.println( name + "说:\"" + words +"\"");
}
}
3.创建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 id="people1" class="com.openlab.pojo.People">
<property name="name" value="张嘎"></property>
<property name="words" value="三天不打小鬼子,手都痒痒!"></property>
</bean>
<bean id="people2" class="com.openlab.pojo.People">
<property name="name" value="Rod"></property>
<property name="words" value="世界上有10种人,认识二进制的和不认识二进制的。"></property>
</bean>
</beans>
4.测试
package com.openlab.test;
import com.openlab.pojo.People;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test01 {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
People people1 = (People) ac.getBean("people1");
people1.talk();
People people2 = (People) ac.getBean("people2");
people2.talk();
}
}
二、作业2
1.创建项目,并导入spring相关的jar包
2.创建ink包
创建ink接口
package com.openlab.ink;
public interface Ink {
String getColor();
}
创建ColorfulInk类
package com.openlab.ink;
public class ColorfulInk implements Ink {
@Override
public String getColor() {
return "彩色墨盒";
}
}
创建GrayInk类
package com.openlab.ink;
public class GrayInk implements Ink {
@Override
public String getColor() {
return "黑白墨盒";
}
}
3.创建paper包
创建Paper接口
package com.openlab.paper;
public interface Paper {
String getSize();
}
创建A4Paper类
package com.openlab.paper;
public class A4Paper implements Paper {
@Override
public String getSize() {
return "A4";
}
}
创建B5Paper类
package com.openlab.paper;
public class B5Paper implements Paper {
@Override
public String getSize() {
return "B5";
}
}
4.创建printer包
创建Printer类
package com.openlab.printer;
import com.openlab.ink.Ink;
import com.openlab.paper.Paper;
public class Printer {
private Ink ink;
private 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 printInfo(){
System.out.println("打印机使用"+ paper.getSize() + "大小的纸张和" + ink.getColor() + "打印了信息" );
}
}
5.配置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 id="printer" class="com.openlab.printer.Printer">
<property name="ink" ref="grayInk"></property>
<property name="paper" ref="A4"></property>
</bean>
<bean id="colorInk" class="com.openlab.ink.ColorfulInk"></bean>
<bean id="grayInk" class="com.openlab.ink.GrayInk"></bean>
<bean id="A4" class="com.openlab.paper.A4Paper"></bean>
<bean id="B5" class="com.openlab.paper.B5Paper"></bean>
</beans>
6.测试
package com.openlab.test;
import com.openlab.printer.Printer;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test02 {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
Printer printer = (Printer) ac.getBean("printer");
printer.printInfo();
}
}
三、作业3
1.创建项目,并导入spring相关的jar包
2.创建pojo包
创建User类
package com.openlab.pojo;
public class User {
private int id;
private String username;
private String password;
private String address;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
3.创建dao包
创建UserDao类
package com.openlab.dao;
import com.openlab.pojo.User;
public class UserDao {
public int insertUser(User user){
System.out.println("向数据库中插入用户成功,用户名为"+user.getUsername() + ",用户地址为" + user.getAddress());
return 1;
}
public int deleteUser(User user){
System.out.println("删除了用户" + user.getUsername());
return 1;
}
}
4.创建service包
创建UserService接口
package com.openlab.service;
import com.openlab.pojo.User;
public interface UserService {
int addUser(User user);
int removeUser(User user);
}
5.创建service.impl包
创建UserServiceImpl类
package com.openlab.service.impl;
import com.openlab.dao.UserDao;
import com.openlab.pojo.User;
import com.openlab.service.UserService;
public class UserServiceImpl implements UserService {
private UserDao userDao;
public UserDao getUserDao() {
return userDao;
}
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
@Override
public int addUser(User user) {
int i = userDao.insertUser(user);
return i;
}
@Override
public int removeUser(User user) {
int i = userDao.deleteUser(user);
return i;
}
}
6.创建logger包
创建UserServiceLogger类
package com.openlab.logger;
import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
public class UserServiceLogger {
private static final Logger log = Logger.getLogger(UserServiceLogger.class);
public void beforePrintLogger(JoinPoint joinPoint){
log.info("调用了" + joinPoint.getTarget() + "的" + joinPoint.getSignature().getName() + "方法,方法参数是"
+ joinPoint.getArgs());
}
public void afterReturningPrintLogger(JoinPoint joinPoint ,Object result){
log.info("调用了" + joinPoint.getTarget() + "的" + joinPoint.getSignature().getName() + "方法,返回值是"
+ result);
}
}
7.配置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 id="userDao" class="com.openlab.dao.UserDao"></bean>
<bean id="userService" class="com.openlab.service.impl.UserServiceImpl">
<property name="userDao" ref="userDao"></property>
</bean>
<bean id="userServiceLogger" class="com.openlab.logger.UserServiceLogger"></bean>
<aop:config>
<aop:pointcut id="addUserPointCut" expression="execution(* com.openlab.service.impl.UserServiceImpl.*(..))"></aop:pointcut>
<aop:aspect ref="userServiceLogger">
<aop:before method="beforePrintLogger" pointcut-ref="addUserPointCut"></aop:before>
<aop:after-returning method="afterReturningPrintLogger" pointcut-ref="addUserPointCut" returning="result"></aop:after-returning>
</aop:aspect>
</aop:config>
</beans>
8.测试
package com.openlab.test;
import com.openlab.pojo.User;
import com.openlab.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test03 {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService = (UserService) ac.getBean("userService");
User user = new User();
user.setId(1);
user.setUsername("zhangsan");
user.setPassword("111");
user.setAddress("China");
userService.addUser(user);
userService.removeUser(user);
}
}