怎么用注解来玩spring?

Spring 针对 Bean 管理中创建对象提供注解

  1. @Component 普通类注解
  2. @Service service 层类注解
  3. @Controller servlet 层类注解
  4. @Repository dao 层类注解
    上面四个注解功能是一样的,都可以用来创建 bean 实例

通过配置文件扫描注解

<?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">
       
    <!--开启扫描注解,扫描文件为 automatic 文件下的所有文件-->
    <context:component-scan base-package="annotation"/>

</beans>

通过注解把bean对象注册到spring容器中

package annotation;

import org.springframework.stereotype.Component;
//这个注解相当于是在xml文件里面配置了bean 并且把名字命名为scanTest
@Component(value = "scanTest")
public class ScanTest {
    
    
    public void info(){
    
    
        System.out.println("这里是scanTest");
    }
}

当然如果我们不指定value的话spring默认会把class名字的第一个字母小写之后的名字当作这个bean的名字

测试

   @Test
    public void test(){
    
    
        ApplicationContext context = new
                ClassPathXmlApplicationContext("beans_annotation.xml");
        ScanTest scanTest = context.getBean("scanTest", ScanTest.class);
        scanTest.info();
    }

在这里插入图片描述

扫描筛选

<!--关闭默认扫描,不扫描全部的类,只扫描包含 Controller 类-->
<context:component-scan base-package="com.test" use-default-filters="false">
 	<context:include-filter type="annotation"
 		expression="org.springframework.stereotype.Controller"/>
	 </context:component-scan>
<!--不扫描包含 Controller 的类-->
	<context:component-scan base-package="com.test">
 	<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>

属性注入

类对象注入

  • @Autowired 根据类型
  • @Qualifier 根据 name 与上边同时使用
  • @Resource 可以根据类型,可以根据 name,不加参数为类型,
    加(name=”name”)为根据 name

普通类型注入

  • @value 注入普通类型

类属性(@autowired @Qualifier @Resource)

这里我们做几个工具类
分别是Dao和Service
Service使用了Dao

Dao

package annotation.Dao;

public interface UserDao {
    
    
    public void add();
}

package annotation.Dao;

import org.springframework.stereotype.Repository;

@Repository(value="userDaoImpl")
public class UserDaoImpl implements UserDao {
    
    
    @Override
    public void add() {
    
    
        System.out.println("UserDao add");
    }
}

Service

package annotation.Service;

public interface UserService {
    
    
    public void add();
}

package annotation.Service;

import annotation.Dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService{
    
    

    @Autowired
    @Qualifier("userDaoImpl")
    //上面两个注解等价于 @Resource(name = "userDaoImpl") 
    private UserDao userDao ;

 
    @Value("这是value注解注入的")
    private String string;
    @Override
    public void add() {
    
    
        userDao.add();
        System.out.println("这是UserServiceImpl重写的add方法");
        System.out.println(string);
    }
}

测试

 @Test
    public void test2(){
    
    
        ApplicationContext context = new ClassPathXmlApplicationContext("beans_annotation.xml");
        UserServiceImpl userServiceImpl = context.getBean("userServiceImpl", UserServiceImpl.class);
        userServiceImpl.add();
    }

在这里插入图片描述

全注解开发

之前配置的扫描包的xml文件也可以被注解所替代
@ComponentScan这个注解可以就是来确定你要扫描那个包的

package annotation;

import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.annotation.ComponentScan;

@Configurable
@ComponentScan(basePackages={
    
    "annotation.Dao","annotation.Service"})
public class componetScan {
    
    

}

测试

   @Test
    public void All_annotation_test(){
    
    
        AnnotationConfigApplicationContext annotation = new AnnotationConfigApplicationContext(componetScan.class);
        UserServiceImpl userServiceImpl = annotation.getBean("userServiceImpl", UserServiceImpl.class);
        userServiceImpl.add();
    }

既然是全注解开发那么就应该把用来读取xml的ClassPathXmlApplicationContext换成用来读取注解的AnnotationConfigApplicationContext
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_47431361/article/details/123429757