初学Spring
2020-12-13 04:45
标签:odi 业务逻辑 one product 就会 解耦 方式 spring注解 利用 jar包自行下载,上面只是截一部分的。 运行结果: 首先在src下创建applicationContext.xml,它是spring的核心配置文件 applicationContext.xml的内容如下: 里面的黑色字体是配置: 创建SpringTest1 运行结果如下: 和上面一样。 我们建立一个新的类,但是这个类要用到别的对象 编写测试类如下: 结果: 这就是我们通常方法,现在看来好像没什么,但是一旦对象个数多了,耦合性就会过高,spring就是来解决这种问题的。 spring方法实现: 首先配置applicationContext.xml 上面红色字体就是对Father类的配置,里面的对象注入要用ref 编写测试代码 结果和上面一样 这样就借助spring实现具有依赖关系的对象之间的解耦 首先一样的我们先配置applicationContext.xml文件 黑色这一行的功能在后面由注解来完成 修改Father类 也可以这样修改 结果都是一样的 效果和@Autowired一样,它们都是对注入对象行为的注解,效果一样都为 一样的先对applicationContext.xml进行配置 作用是告诉spring,bean都放在cn.yf.pojo这个包下面 然后对Person类进行修改 对Father类的配置 然后再次运行测试代码结果如下: 因为没有设置初值,所以默认为0或者null 什么是AOP:面向切面编程,利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各个部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。 先设计业务类代码如下 再设计辅助功能类(切面) 配置applicationContext.xml 测试代码: 测试结果: 使用@Component("s") 注解ProductService 类 注解配置切面 对applicationContext.xml进行配置 运行结果一样: 初学Spring 标签:odi 业务逻辑 one product 就会 解耦 方式 spring注解 利用 原文地址:https://www.cnblogs.com/Vamps0911/p/11119527.html1.首先我们先建立一个java项目,导入spring相关的jar包
2.1 准备pojo
package cn.yf.pojo;
public class Person {
private int age;
private String name;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Person [age=" + age + ", name=" + name + "]";
}
}
2.2 按照常规方法创建对象
1 package cn.yf.test;
2
3 import cn.yf.pojo.Person;
4
5 public class Test1 {
6
7 public static void main(String[] args) {
8 // TODO Auto-generated method stub
9 Person person = new Person();
10 person.setAge(20);
11 person.setName("tht");
12 System.out.println(person.getAge());
13 System.out.println(person.getName());
14 }
15
16 }
20
tht
2.3 用spring来创建对象
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"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-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/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
package cn.yf.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.yf.pojo.Person;
public class TestSpring1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"});
Person p = (Person)context.getBean("p");
System.out.println(p.getAge());
System.out.println(p.getName());
}
}
20
tht
3 spring注入对象
package cn.yf.pojo;
public class Father {
private int age;
private String name;
private Person person;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
@Override
public String toString() {
return "Father [age=" + age + ", name=" + name + ", person=" + person + "]";
}
}
package cn.yf.test;
import cn.yf.pojo.Father;
import cn.yf.pojo.Person;
public class Test2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Person person = new Person();
person.setAge(20);
person.setName("tht");
Father father = new Father();
father.setAge(50);
father.setName("yf");
father.setPerson(person);
System.out.println(father.getAge());
System.out.println(father.getName());
System.out.println(father.getPerson());
}
}
50
yf
Person [age=20, name=tht]
package cn.yf.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.yf.pojo.Father;
public class TestSpring2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"});
Father father = (Father)context.getBean("f");
System.out.println(father.getAge());
System.out.println(father.getName());
System.out.println(father.getPerson());
}
}
50
yf
Person [age=20, name=tht]
4 spring注解方式IOC/DI
4.1 @Autowired
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"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-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/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
context:annotation-config/>
bean name="p" class="cn.yf.pojo.Person">
property name="age" value="20">property>
property name="name" value="tht">property>
bean>
bean name="f" class="cn.yf.pojo.Father">
property name="age" value="50">property>
property name="name" value="yf">property>
bean>
1 @Autowired
2 private Person person;
1 @Autowired
2 public void setPerson(Person person) {
3 this.person = person;
4 }
50
yf
Person [age=20, name=tht]
4.2 @Resource
50
yf
Person [age=20, name=tht]
4.3 @Component
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"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-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/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
context:component-scan base-package="cn.yf.pojo">context:component-scan>
beans>
@Component("p")
public class Person
@Component("f")
public class Father
0
null
Person [age=0, name=null]
5.1 Spring AOP
1 package cn.yf.service;
2
3 import org.springframework.stereotype.Component;
4
5 @Component("s")
6 public class ProductService {
7
8 public void doSomeService(){
9 System.out.println("doSomeService");
10 }
11
12 }
1 package cn.yf.aspect;
2
3 import org.aspectj.lang.ProceedingJoinPoint;
4
5 public class LoggerAspect {
6
7 public Object log(ProceedingJoinPoint joinPoint) throws Throwable{
8 System.out.println("start log:" + joinPoint.getSignature().getName());
9 //用于执行核心功能的代码
10 Object object = joinPoint.proceed();
11 System.out.println("end log:" + joinPoint.getSignature().getName());
12 return object;
13 }
14
15 }
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"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-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/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
bean name="s" class="cn.yf.service.ProductService">bean>
bean id="loggerAspect" class="cn.yf.aspect.LoggerAspect">bean>
aop:config>
aop:pointcut expression="execution(* cn.yf.service.ProductService.*(..))" id="loggerCutpoint"/>
aop:aspect id="logAspect" ref="loggerAspect">
aop:around pointcut-ref="loggerCutpoint" method="log"/>
aop:aspect>
aop:config>
beans>
package cn.yf.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.yf.service.ProductService;
public class TestSpring3 {
public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"});
ProductService s = (ProductService)context.getBean("s");
s.doSomeService();
}
}
start log:doSomeService
doSomeService
end log:doSomeService
5.2 Spring注解方式AOP
@Component("s")
public class ProductService
@Aspect
@Component
public class LoggerAspect {
@Around(value = "execution(* cn.yf.service.ProductService.*(..))")
public Object log(ProceedingJoinPoint joinPoint) throws Throwable{
System.out.println("start log:" + joinPoint.getSignature().getName());
//用于执行核心功能的代码
Object object = joinPoint.proceed();
System.out.println("end log:" + joinPoint.getSignature().getName());
return object;
}
}
context:component-scan base-package="cn.yf.aspect">context:component-scan>
context:component-scan base-package="cn.yf.service">context:component-scan>
aop:aspectj-autoproxy/>
start log:doSomeService
doSomeService
end log:doSomeService