一、Spring的配置
1.spring2.5后=xml+annotation
<bean id="" class="" init-method="" destroy-method="" scope="" autowire="">
<property></property>
<constructor-arg></constructor-arg>
</bean>
2 注解:
2.1 注入类:
位置:类
语法:@Component
(value="注入容器中的id,如果省略id为类名且首字母小写,value属性名称可以省略")
eg:@Component
Class User{}
@Component(value = "Stu")
public class Student {}
给配置文件加包扫描
<context:component-scan base-package="com.ly"></context:component-scan>
@Repository=====>注入数据访问层
@Service========>注入业务层
@Controller=====>注入控制层
与@Component功能语法一致
@Repository
@Primary
public class StudentDaoImpl implements IStudentDao{
public void save() {
System.out.println("dao1新增");
}
}
@Service
@Resource(name = "studentDaoImpl")
public class StudentServiceImpl implements IStudentService{
// @Qualifier(value = "studentDaoImpl")
@Autowired
IStudentDao dao;
public void save() {
System.out.println("service 新增");
}
}
@Controller
public class StudentControllerImpl implements IStudentController{
@Autowired
IStudentService service;
public void save() {
System.out.println("controller新增");
}
}
2.2注入数据
@Value
修饰:成员变量
语法:@Value("数据内容")
@Value("${动态获取}")
@Component(value = "Stu")
public class Student {
@Value("${msg1}")
private String sname;
@Value("${msg2}")
private int sage;
}
配置文件
<context:component-scan base-package="com.ly"></context:component-scan>
<context:component-scan base-package="com.ly.dao"></context:component-scan>
<context:component-scan base-package="com.ly.service"></context:component-scan>
<context:component-scan base-package="com.ly.controller"></context:component-scan>
<context:property-placeholder location="classpath:message.properties"></context:property-placeholder>
@Autowired
语法:@Autowired(required = "true-默认、false、是否必须进行装配")
修饰:成员变量
含义:按照通过构造方法进行“类型装配”,构造方法可以省略
注意:
1.若容器中有一个类型可以与之匹配则装配成功,若没有一个类型可以匹配则报错
NoSuchBeanDefinitionException
2.若容器中有多个类型可以与之匹配,则自动切换为按照名称装配,若名称没有对应,则报错
NoUniqueBeanDefinitionException
2.3.其他注解
@Primary
含义:首选项,当类型冲突的情况下,此注解修饰的类被列为首选
修饰:类
注意:不能单独使用,必须与@Component....联合使用
@Repository
@Primary
public class StudentDaoImpl implements IStudentDao{
public void save() {
System.out.println("dao1新增");
}
}
@Qualiffier(value="名称")
含义:按照名称装配
修饰:成员变量
注意:不能单独使用,必须与@Autowired联合使用
@Resource(name="名称")
含义:配置类的作用域
修饰:成员变量
注意:单独使用
@Scope
含义:配置类的作用域
修饰:类
注意:不能单独使用,必须与@Component联合使用
@Scope("prototype")
@Scope("singleton")
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
@Scope(ConfigurableBeanFactory.SCOPE_SINGLETON)
3.spring3.0后==annotation+JavaConfig配置类
@Configuration
作用:指定当前类是一个配置类
细节:当配置类作为AnnotationConfigApplicationContext对象创建的参数时,该注解可以不写。
@ComponentScan
作用:用于通过注解指定spring在创建容器时要扫描的包
@Bean
作用:用于把当前方法的返回值作为bean对象存入spring的容器中
属性:name:用于指定bean的id。当不写时,默认值是当前方法的名称
@Import
作用:用于导入其他的配置类
属性:value:用于指定其他配置类的字节码。
@PropertySource
作用:用于指定properties文件的位置
4.注解类项目
ApplicationConfig.java
@Configuration
@Import(OtherConfig.class)
@ComponentScan(basePackages = "com.ly")
public class ApplicationConfig {
}
package com.ly.config;
import com.ly.pojo.Teacher;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import java.util.Date;
@Configuration
@PropertySource(value = "classpath:message.properties")
public class OtherConfig {
@Bean
public Teacher teacher1(){
return new Teacher();
}
@Bean("teach")
public Teacher teacher2(){
return new Teacher();
}
@Bean("data")
public Date data(){
return new Date();
}
}
因篇幅问题不能全部显示,请点此查看更多更全内容