使用注解开发 & JavaConfig 配置

在spring之后,如果使用注解开发,就要导入aop的包

 

 

 在配置文件中,要引入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
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd">
 
  <!--开启注解支持    -->
  <context:annotation-config/>
   <!--指定注解扫描包-->
<context:component-scan base-package="com.kuang.pojo"/>


</beans>

在指定包下编写类,增加注解

@Component// 相当于配置文件中 <bean id="user" class="当前注解的类"/>
public class User {
   public String name = "小明";
}

测试

@Test
public void test(){
   ApplicationContext applicationContext =
       new ClassPathXmlApplicationContext("applicationContext.xml");
   User user = (User) applicationContext.getBean("user");
   System.out.println(user.name);
}

 

 

属性注入

  1、可以不用set方法,直接在属性名上添加 @value("值")

  

@Component("user")
// 相当于配置文件中 <bean id="user" class="当前注解的类"/>
public class User {
   @Value("王野")
   // 相当于配置文件中 <property name="name" value="王野"/>
   public String name;
}

  2、当然,也可以在set方法上添加 @value("值")

 

@Component("user")
public class User {

   public String name;

   @Value("王野")
   public void setName(String name) {
       this.name = name;
  }
}

 

衍生注解

@Component三个衍生注解

  为了更好的饿进行分层开发,Spring可以使用其他三个注解,目前他们的功能都是一样的

  • @Controller : web层
  • @Service : service层
  • @Repository :dao层
  • 自动装配注解@Autowired

作用域  @scope

  • singleto:默认的,Spring会采用单例模式创建这个对象。关闭工厂,所有的对象都会销毁
  • prototype: 多例模式。关闭工厂,所有的对象不会销毁。内部的垃圾回收机制会回收
@Controller("user")
@Scope("prototype")
public class User {
   @Value("王野")
   public String name;
}

XML与注解比较

  • XML可以适用任何场景 ,结构清晰,维护方便

  • 注解不是自己提供的类使用不了,开发简单方便

xml与注解整合开发 :推荐最佳实践

  • xml管理Bean

  • 注解完成属性注入

  • 使用过程中, 可以不用扫描,扫描是为了类上的注解

 

基于Java类进行配置

JavaConfig原来是spring 的一个子项目,它通过Java类的方式提供Bean的定义信息,在Spring4的版本,JavaConfig已正式成为Spring4的核心功能

1、编写一个实体类

package com.wang.pojo;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@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 + '\'' +
                '}';
    }
}

2、新建一个config配置包,编写一个Config配置类

package com.wang.config;

import com.wang.pojo.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class WangConfig {
    @Bean
    public User getUser(){
        return new User();
    }
}

3、测试

import com.wang.config.WangConfig;
import com.wang.pojo.User;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Tt {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(WangConfig.class);
        User getUser = (User) context.getBean("getUser");
        System.out.println(getUser.getName());
    }
}
@Import(MyConfig2.class)  //导入合并其他配置类,类似于配置文件中的 inculde 标

 

posted @ 2021-01-27 14:00  IanW  阅读(128)  评论(0编辑  收藏  举报