Spring-注解开发

1.首先在配置文件beans.xml中增加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/>
    <!--扫描com.kuang包下的注解-->
    <context:component-scan base-package="com.kuang"/>

</beans>

2.创建实体类User

@Component("user")
@Scope("singleton")
//相当于<bean id="user" class=""/>
public class User {
    
    
    @Value("1")
    private int id;
    @Value("张三")
    //<property name="name" value="张三"/>
    private String name;

    @Override
    public String toString() {
    
    
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}

3.测试

public class MyTest {
    
    

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

@Component相当于配置一个bean
@Value 属性注入

衍生注解
@Component的三个衍生注解
在MVC模式下,分层开发,为了区别每一层,分别使用不同注解标记,但功能同@Component相同

  • Controller层:@Controller
  • Service层:@Service
  • Dao层:@Reposity
    在类上添加了这些注解后,就相当于将这些类交给spring托管了。

自动装配注解

  • @Autowire
    根据类型自动装配,搭配@Qualifier可以根据名字进行自动装配
  • @Nullable 可以为空

作用域
@Scope

  • singleton:单例模式
  • prototype:多例模式

总结
XML与注解比较

  • XML可以应用于任何场景,结构清晰,维护方便
  • 注解只能在自己创建的类上使用,想托管依赖包的类还得用XML(后面整合SSM框架会用到)

XML与注解开发推荐

  • xml管理bean
  • 注解完成属性注入
  • 使用过程中,可以不用扫描,扫描是为了类上的注解
<context:annotation-config/>  

作用

  • 进行注解驱动注册,使注解生效
  • 用于激活那些已经在spring容器中注册过的bean上面的注解,也就是向spring的注册
  • 如果不扫描包,就要手动配置bean
  • 如果不加注解驱动,则注入的值为null

不使用xml,基于java类进行配置
JavaConfig 原来是 Spring 的一个子项目,它通过 Java 类的方式提供 Bean 的定义信息,在 Spring4 的版本, JavaConfig 已正式成为 Spring4 的核心功能 。
1.写实体类

@Component  //将这个类标注为Spring的一个组件,放到容器中!
public class Dog {
    
    
   public String name = "dog";
}

2.建一个config包,编写一个MyConfig配置类

@Configuration  //代表这是一个配置类
public class MyConfig {
    
    

   @Bean //通过方法注册一个bean,这里的返回值就Bean的类型,方法名就是bean的id!
   public Dog dog(){
    
    
       return new Dog();
  }

}

3.测试

@Test
public void test2(){
    
    
   ApplicationContext applicationContext =
           new AnnotationConfigApplicationContext(MyConfig.class);
   Dog dog = (Dog) applicationContext.getBean("dog");
   System.out.println(dog.name);
}

合并其他配置类
1.再新建一个配置类

@Configuration  //代表这是一个配置类
public class MyConfig2 {
    
    
}

2.合并

@Configuration
@Import(MyConfig2.class)  //导入合并其他配置类,类似于配置文件中的 import标签
public class MyConfig {
    
    

   @Bean
   public Dog dog(){
    
    
       return new Dog();
  }

}

猜你喜欢

转载自blog.csdn.net/qq_42665745/article/details/112337828