Spring笔记

特点

  • 控制反转IOC、面向切面AOP
  • 配置地狱

控制反转:第三方(IOC容器)将对象setter注入,而不用在程序写死

spring开发的pom文件

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>5.1.9.RELEASE</version>
</dependency>

webmvc包含spring开发的常用包,idea会自动引入依赖包

spring 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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="user" class="com.yangxudong.spring.pojo.User">
        <property name="name" value="yangxudong"/>
    </bean>
</beans>
ApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
User user = (User) context.getBean("user");
System.out.println(user.getName());

jdk1.5 spring2.5开始支持注解

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:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">

    <!-- 自动扫描 -->
    <context:component-scan base-package="com.yangxudong.spring.annotation.pojo"/>
    <!-- 支持注解 -->
    <context:annotation-config/>

</beans>
@Component
@Scope("prototype")
public class People {
    @Autowired
    private Cat cat;
    @Autowired
    @Qualifier("dog")
    private Dog dog;
    @Resource
    private Bird bird;
    @Value("yangxudong")
    private String name;

@Component 类交由spring管理,它的衍生注解有@Controller @Service @Repository
@Scope 指定作用域
@Autowired 默认byType,找不到再byName
@Resource 默认byName,找不到再byType
@Autowired 不能唯一确认时,通过@Qualifier(“dog”)指定
@Value 设置值

使用注解替代配置文件

@Configuration
@ComponentScan("com.yangxudong.spring.annotation.pojo")
public class SpringConfig {
    @Bean
    public Student getStudent() {
        return new Student("test");
    }
}
ApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(SpringConfig.class);
Student student = annotationConfigApplicationContext.getBean("getStudent", Student.class);
System.out.println(student.getName());

@Configuration @ComponentScan @Bean 可以代替application.xml,做到0配置

动态代理

public class ProxyInvocationHandler implements InvocationHandler {
    private Object target;

    public void setTarget(Object target) {
        this.target = target;
    }

    public Object getProxy() {
        return Proxy.newProxyInstance(this.getClass().getClassLoader(), target.getClass().getInterfaces(), this);
    }

    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        before();
        method.invoke(target,args);
        after();
        return null;
    }

    private void before() {
        System.out.println("方法执行前");
    }

    private void after() {
        System.out.println("方法执行后");
    }
}
public static void main(String[] args) {
    ProxyInvocationHandler proxy = new ProxyInvocationHandler();
    proxy.setTarget(new UserServiceImpl());
    UserService userService = (UserService) proxy.getProxy();
    userService.add();
    userService.delete();
}

AOP的pom文件

<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.9.4</version>
</dependency>

AOP配置方式一

<?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
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd">

    <bean id="userService" class="com.yangxudong.aop.service.UserServiceImpl"/>
    <bean id="log" class="com.yangxudong.aop.log.Log"/>

    <aop:config>
        <aop:pointcut id="pointcut" expression="execution(* com.yangxudong.aop.service.UserServiceImpl.*(..))"/>
        <aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
    </aop:config>
</beans>
public class Log implements MethodBeforeAdvice {
    public void before(Method method, Object[] args, Object target) throws Throwable {
        System.out.println(target.getClass().getName()+"的"+method.getName()+"方法被执行了");
    }
}

AOP配置方式二

<?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
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd">

    <bean id="userService" class="com.yangxudong.aop.service.UserServiceImpl"/>
    <bean id="print" class="com.yangxudong.aop.log.Print"/>

    <aop:config>
        <aop:aspect ref="print">
            <aop:pointcut id="point" expression="execution(* com.yangxudong.aop.service.UserServiceImpl.*(..))"/>
            <aop:before method="before" pointcut-ref="point"/>
            <aop:after method="after" pointcut-ref="point"/>
        </aop:aspect>
    </aop:config>
</beans>
public class Print {
    public void before() {
        System.out.println("方法执行前");
    }

    public void after() {
        System.out.println("方法执行后");
    }
}

AOP配置方式三

<?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
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd">
        
    <bean id="annotationAop" class="com.yangxudong.aop.log.AnnotationAop"/>
    <aop:aspectj-autoproxy/>
</beans>
@Aspect
public class AnnotationAop {
    @Around("execution(* com.yangxudong.aop.service.UserServiceImpl.delete(..))")
    public void around(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("环绕前");
        pjp.proceed();
        System.out.println(pjp.getSignature().toString());
        System.out.println("环绕后");
    }
}

事务

ACID原则:
原子性、一致性、隔离性、持久性。
未完待续。。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值