IOC相关注解
@Component @Service @Controller @Repository
DI相关注解
@Value 主要用于String,Integer等可以只直接赋值的属性注入,不依赖setter方法,支持SpEL表达式,例如
@Autowired Spring会给加了该注解的属性自动注入数据类型相同的对象注入。
@Qualifier 如果相同类型的bean在容器中有多个时,单独使用@Autowired就不能满足要求,这时候可以再加上@Qualifier来指定bean的名字从容器中获取bean注入。
package com.sangeng.service.impl;
import com.sangeng.dao.UserDao;
import com.sangeng.service.UserService;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.omg.PortableInterceptor.INACTIVE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
@Service("userService")
@Data
@NoArgsConstructor
@AllArgsConstructor
public class UserServiceImpl implements UserService {
@Autowired
@Qualifier("userDao2")
private UserDao userDao;
@Value("199")
private int num;
@Value("三更草堂")
private String str;
@Value("#{19+3}")
private Integer age;
public void show() {
userDao.show();
}
}
xml配置文件相关注解
@Configuration 标注在类上,表示当前类是一个配置类,我们可以用注解类来完全替换掉xml配置文件。注意:如果使用配置类替换了xml配置,spring容器要使用:AnnotationConfigApplicationContext
@ComponentScan 可以用来替代context:component-scan标签来配置组件扫描。
例如
package com.sangeng.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan(basePackages = "com.sangeng")
public class ApplicationConfig {
}
@Bean 可以用来代替bean标签,主要用于第三方类的注入。例如
package com.sangeng.config;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan(basePackages = "com.sangeng")
public class ApplicationConfig {
@Bean
public DruidDataSource getDataSource(){
DruidDataSource druidDataSource = new DruidDataSource();
druidDataSource.setDriverClassName(driverClassName);
druidDataSource.setUsername(username);
druidDataSource.setUrl(url);
druidDataSource.setPassword(password);
return druidDataSource;
}
}
@PropertySource 可以用来代替 context:property-placeholder,让Spring读取指定的properties文件,然后可以使用@Value来获取读取到的值。例如
package com.sangeng.config;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
@Configuration
@ComponentScan(basePackages = "com.sangeng")
@PropertySource("jdbc.properties")
public class ApplicationConfig {
@Value("${jdbc.driver}")
private String driverClassName;
@Value("${jdbc.url}")
private String url;
@Value("${jdbc.username}")
private String username;
@Value("${jdbc.password}")
private String password;
@Bean
public DruidDataSource getDataSource(){
DruidDataSource druidDataSource = new DruidDataSource();
druidDataSource.setDriverClassName("com.mysql.jdbc.Driver");
druidDataSource.setUsername("root");
druidDataSource.setUrl("jdbc:mysql://localhost:3306/choose");
druidDataSource.setPassword("123456789");
return druidDataSource;
}
}
jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/choose
jdbc.username=root
jdbc.password=123456789