版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
一、注解基本使用介绍
使用annotation注释替换之前较为复杂的spring.xml管理代码的方式。
二、环境
1.Pom
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
<!--单元测试-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
三、代码实现
1.代码
(1)Bean1类
package com.spring.ioc;
/**
* Created by Administrator on 2019/10/26.
*/
public class Bean1 {
}
(2)MyConfiguration类(相当于之前的spring.xml文件)
package com.spring.ioc;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Bean;
/**
* Created by Administrator on 2019/10/26.
*/
@Configuration
public class MyConfiguration {
@Bean(value = "bean1")
public Bean1 bean1(){
return new Bean1();
}
}
2.测试
package com.spring.ioc;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
* Created by Administrator on 2019/10/26.
*/
public class testCode {
@Test
public void test(){
ApplicationContext context=new AnnotationConfigApplicationContext(MyConfiguration.class);
//bean1
Bean1 bean1=context.getBean("bean1",Bean1.class);
System.out.println("bean1 = " + bean1);
}
}
结果:
bean1 = com.spring.ioc.Bean1@36f0f1be
四、简化上述代码
1.MyConfiguration.class类
在配置文件上面添加ComponentScan注解,扫描指定包下面的实例对象
package com.spring.ioc;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Bean;
/**
* Created by Administrator on 2019/10/26.
*/
@Configuration
@ComponentScan(value = "com.spring.ioc")
public class MyConfiguration {
}
2.Bean1.class类
添加@Component注解,说明是一个交由spring管理的Bean对象。备注:value可以指定对应id,默认id是方法的Bean的第一个字母小写。
package com.spring.ioc;
import org.springframework.stereotype.Component;
/**
* Created by Administrator on 2019/10/26.
*/
@Component(value = "bean2")
public class Bean1 {
}
3.测试代码
package com.spring.ioc;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
* Created by Administrator on 2019/10/26.
*/
public class testCode {
@Test
public void test(){
ApplicationContext context=new AnnotationConfigApplicationContext(MyConfiguration.class);
//bean1
Bean1 bean1=context.getBean("bean2",Bean1.class);
System.out.println("bean1 = " + bean1);
}
}
结果
bean1 = com.spring.ioc.Bean1@2145433b
五、注解
1.@Controller:被标注在Controller层
2.@Service:被标注在Service层
3.@Repository:被标注在Dao层
4.@component:通用型注解
六、Bean添加别名
1.修改配置文件
只能用比较复杂的注入方式
package com.spring.ioc;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Bean;
/**
* Created by Administrator on 2019/10/26.
*/
@Configuration
//@ComponentScan(value = "com.spring.ioc")
public class MyConfiguration {
@Bean(value = {"bean2","bean3"})
public Bean1 bean1(){
return new Bean1();
}
}
2.Bean1类
package com.spring.ioc;
import org.springframework.stereotype.Component;
/**
* Created by Administrator on 2019/10/26.
*/
//@Component(value = "bean2")
public class Bean1 {
}
4.测试代码
package com.spring.ioc;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
* Created by Administrator on 2019/10/26.
*/
public class testCode {
@Test
public void test(){
ApplicationContext context=new AnnotationConfigApplicationContext(MyConfiguration.class);
//bean1
Bean1 bean1=context.getBean("bean2",Bean1.class);
System.out.println("bean1 = " + bean1);
//bean3
Bean1 bean3=context.getBean("bean3",Bean1.class);
System.out.println("bean3 = " + bean3);
}
}
测试代码
bean1 = com.spring.ioc.Bean1@36f0f1be
bean3 = com.spring.ioc.Bean1@36f0f1be