在spring4以后,想要使用注解形式,必须得要引入aop的包java
在配置文件当中,还得要引入一个context约束web
<?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 http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> </beans>
咱们以前都是使用 bean 的标签进行bean注入,可是实际开发中,咱们通常都会使用注解!spring
一、配置扫描哪些包下的注解app
<!--指定注解扫描包--> <context:component-scan base-package="com.zhonghu.pojo"/>
二、在指定包下编写类,增长注解ide
@Component("user") // 至关于配置文件中 <bean id="user" class="当前注解的类"/> public class User { public String name = "冢狐"; }
三、测试测试
@Test public void test(){ ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml"); User user = (User) applicationContext.getBean("user"); System.out.println(user.name); }
使用注解注入属性this
一、能够不用提供set方法,直接在直接名上添加@value("值")prototype
@Component("user") // 至关于配置文件中 <bean id="user" class="当前注解的类"/> public class User { @Value("冢狐") // 至关于配置文件中 <property name="name" value="冢狐"/> public String name; }
二、若是提供了set方法,在set方法上添加@value("值");code
@Component("user") public class User { public String name; @Value("冢狐") public void setName(String name) { this.name = name; } }
咱们这些注解,就是替代了在配置文件当中配置步骤而已!更加的方便快捷!component
@Component三个衍生注解
为了更好的进行分层,Spring可使用其它三个注解,功能同样,目前使用哪个功能都同样。
写上这些注解,就至关于将这个类交给Spring管理装配了!
@Autowired:自动装配经过类型,找不到再找名称
@Nullable:自动标记了这个注解,说明这个注解能够为null
@Resource:自动装配经过名字,找不到再类型
@Controller("user") @Scope("prototype") public class User { @Value("冢狐") public String name; }
XML与注解比较
xml与注解整合开发 :推荐最佳实践
<context:annotation-config/>
做用:
咱们如今要彻底不使用Spring的xml配置,全权交给java来处理
JavaConfig 原来是 Spring 的一个子项目,它经过 Java 类的方式提供 Bean 的定义信息,在 Spring4 的版本, JavaConfig 已正式成为 Spring4 的核心功能 。
// 此注解代表一个类做为组件类,并告知Spring要为这个类建立bean @Component public class User { private String name; public String getName() { return name; } @Value("冢狐") public void setName(String name) { this.name = name; } @Override public String toString() { return "User{" + "name='" + name + '\'' + '}'; } }
// 这个也会Spring容器托管,注册到容器中,由于它自己就是一个@Conmponent // @Configuration表明这是一个配置类,和咱们以前看到的beans.xml是同样的 @Configuration @ComponentScan("com.zhonghu.pojo") public class MyConfig { // 注册一个bean,就至关于咱们以前写的一个bean标签 // 这个方法的名字,至关于咱们bean标签中的id // 这个方法的返回值,至关于咱们bean标签中的class属性 @Bean public User getUser(){ // 就是返回要注入的bean的对象 return new User(); } }
public class MyTest { public static void main(String[] args) { // 若是咱们完成使用了配置类方式去实现,咱们就只能经过AnnotationConfig上下文来获取容器,经过配置类的class来加载对象 ApplicationContext context = new AnnotationConfigApplicationContext(MyConfig.class); User getUser = (User) context.getBean("getUser"); System.out.println(getUser.getName()); } }
四、成功输出结果!
导入其余配置如何作呢?
一、咱们再编写一个配置类!
@Configuration //表明这是一个配置类 public class MyConfig2 { }
二、在以前的配置类中咱们来选择导入这个配置类
@Configuration @Import(MyConfig2.class) //导入合并其余配置类,相似于配置文件中的 inculde 标签 public class MyConfig { @Bean public User getUser(){ // 就是返回要注入的bean的对象 return new User(); } }
欢迎关注公众号“Java冢狐”获取最新消息