Spring

IOC

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

    <!-- id相当于变量名  -->
    <bean id="hello" class="com.ash.springtest01.hello">
        <property name="value" value="Spring"></property>
    </bean>
    
</beans>
p命名空间 和 c命名空间

p: xmlns:p="http://www.springframework.org/schema/p"(set注入)

<bean name="p-namespace" class="com.example.ExampleBean"
    p:email="someone@somewhere.com"/>

c: xmlns:c="http://www.springframework.org/schema/c"(构造器)

<bean id="beanOne" class="x.y.ThingOne" c:thingTwo-ref="beanTwo"
    c:thingThree-ref="beanThree" c:email="something@somewhere.com"/>

bean的作用域

在这里插入图片描述

  1. 单例模式(默认机制)
<bean id="accountService" class="com.something.DefaultAccountService"/>

<!-- the following is equivalent, though redundant (singleton scope is 
the default) -->
<bean id="accountService" class="com.something.DefaultAccountService" 
scope="singleton"/>
  1. 原型模式
    每次从容器中get到的都是新对象
<bean id="accountService" class="com.something.DefaultAccountService" 
scope="prototype"/>
  1. 其他只能在web开发中使用到

Bean的自动装配

Spring中的三种装配方式:

  • 在xml中显示配置
  • 在java中显示配置
  • 隐式自动装配bean
bean的自动装配
  • byName
	<!-- people中getter的名称在容器中查找匹配的id -->
    <bean id="people" class="com.ash.springtest01.People" autowire="byName">
        <property name="name" value="111"/>
    </bean>
  • byType
	<!-- people中参数类型在容器中查找匹配的bean -->
    <bean id="people" class="com.ash.springtest01.People" autowire="byType">
        <property name="name" value="111"/>
    </bean>
注解自动装配

导入context约束、配置注解的支持

<?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:annotation-config/>

</beans>
@Autowired

通过byType的方式实现,对象必须存在;
若找到的对象不唯一则使用 @Qualifier(value=“xxx”)

@Resource

默认通过byName,找不到则byType,都找不到时报错

@Nullable

这个字段可以为null

@Component

表示这个类被spring管理了,等价于在applicationContext.xml中的<bean id="xxx" class="com.ash.test.xxx">
内部set方法可使用@Value设定值

@Component
public class People {
    String name;
    
    @Value("abc")
    public void setName(String name) {
        this.name = name;
    }
}
  • @Repository(dao)
  • @Service(service)
  • @Controller(controller)
    这四个注解功能一样,都代表将某个类注册到spring中装配bean

xml与注解最佳使用:xml用来管理bean,注解只负责完成属性的注入

如果完全使用注解做配置类,则只能通过AnnotationConfig上下文来获取容器,通过配置类的class对象加载
<配置文件>:

package com.ash.springtest01;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

@Configuration
@ComponentScan("com.ash.springtest01")
@Import(People.class)
public class testConfig {
    @Bean
    public People people(){
        return new People();
    }
}

<测试>:

import com.ash.springtest01.People;
import com.ash.springtest01.testConfig;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class MyTest {
    ApplicationContext context = new  
    			AnnotationConfigApplicationContext(testConfig.class);
    People p = (People) context.getBean("people");
}

AOP

pom.xml

maven添加依赖

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

applicationContext.xml

    <!-- aop注解支持 -->
    <aop:aspectj-autoproxy/>

*.class

package com.ash.springtest01;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

@Aspect
public class AnnotationPointCut {
    @Before("execution(* com.ash.springtest01.*(..))")
    public void A(){
        System.out.println("---before---");
    }
    
    @After("execution(* com.ash.springtest01.*(..))")
    public void B(){
        System.out.println("---after---");
    }
    
    @Around("execution(* com.ash.springtest01.*(..))")
    public void C(ProceedingJoinPoint jp) throws Throwable {
        System.out.println("环绕前");
        Object proceed = jp.proceed();//执行方法
        System.out.println("环绕后");
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值