spring系列 注解开发

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6

@Component

bean的类上加注解@Componentvalue是否设置值取决于用id获取还是根据class名获取id获取就要给一个value对应id值。

xml中要先指定命名空间在接component-scan开启包扫描后面接要扫描那个路径下的包

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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:component-scan base-package="com.xxz"/>

</beans>

Spring提供@Component注解的三个衍生注解

@Controller用于表现层bean定义

@Service用于业务层bean定义

@Repository用于数据层bean定义

@Configuration

用于设定当前类为配置类

作用是spring对xml结构的简化xml结构就是下面这块

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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>

@ComponentScan

用于设定扫描路径此注解只能添加一次多个数据用数组格式

也就是简化xml中的

<context:component-scan base-package="com.xxz"/>

bean模式@Scope("singleton")

bean生命周期@PostConstruct、@PreDestroy

public class BookDaoImpl implements BookDao {    

        @PostConstruct    

        public void init(){        

                System.out.println("book init ...");    

        }    

        @PreDestroy    

        public void destroy(){        

                System.out.println("book destory ...");    

        }

}

自动装配@Autowired、@Qualifier

自动装配默认使用无参构造方法创建对象并基于反射创建对象因此无需提供set方法之前的byType模式需要set方法是因为有可能方法定义了private框架考虑的是不能越俎代庖现在为了方便都可以反射了。

@Autowired是基于 byType模式的需要bean唯一当有多个相同类型的bean需要初始化时会报错为了解决这个问题可以采用下面的@Qualifier注解bean的类上里面配上bean的id@Repository("bookDao")@Qualifier配上对应的id

@Qualifier不能单独使用要依赖于@Autowired没有的话没有注入调用会报空指针。

值注入@Value

读取配置文件@PropertySource

多个路径书写为字符串数组格式这里不支持使用通配符会将*视为文件名的一部分lasspath:可加可不加。

引入起来配置类@Import

配置信息可能会有很多所以我们用一个配置类专门管理用@Import引入其他配置类。

注入返回方法对象@Bean

表示当前方法的返回值是一个bean。

@Bean修饰的方法形参根据类型自动装配

注解的综合用法

定义bean

@Repository

//@Component("bookDao")

@Scope("singleton")

public class BookDaoImpl implements BookDao {    

        @Value("${name}")   

        private String connectionNum;

        @PostConstruct    

        public void init(){        

                System.out.println("book init ...");    

        }    

        @PreDestroy    

        public void destroy(){        

                System.out.println("book destory ...");    

        }

}

注入

@Service

public class BookServiceImpl implements BookService {    

        @Autowired   

        //@Qualifier("bookDao1") 

        private BookDao bookDao;  

        public void setBookDao(BookDao bookDao) {        

                this.bookDao = bookDao;    

        }    

        public void save() {        

                System.out.println("book service save ...");        

                bookDao.save();    

        }

}

@Configuration声明当前类为Spring配置类

@Configuration
//设置bean扫描路径多个路径书写为字符串数组格式
@ComponentScan({"com.xxz.service","com.xxz.dao"})

@PropertySource("classpath:jdbc.properties")

@Import({JdbcConfig.class})
public class SpringConfig {
}

public class JdbcConfig {
    //1.定义一个方法获得要管理的对象
    @Value("com.mysql.jdbc.Driver")
    private String driver;
    @Value("jdbc:mysql://localhost:3306/spring_db")
    private String url;
    @Value("root")
    private String userName;
    @Value("root")
    private String password;

    //当有依赖对象注入需求时由于总配置类开启了包扫描所以这里只需要给BookDao bookDao框架就帮我们自动注入了。
    @Bean
    public DataSource dataSource(BookDao bookDao){
        System.out.println(bookDao);
        DruidDataSource ds = new DruidDataSource();
        ds.setDriverClassName(driver);
        ds.setUrl(url);
        ds.setUsername(userName);
        ds.setPassword(password);
        return ds;
    }
}

jdbc.properties

name=888

启动类中采用注解容器

ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);

DataSource dataSource = ctx.getBean(DataSource.class);
System.out.println(dataSource);

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6
标签: Spring