Spring基于配置文件 为了让属性(对象的引用)注入更加的简单.则推出了自动装配模式.
<beans xmlns="http://www.springframework.org/schema/beans"
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">
<bean id="user" class="com.jt.pojo.User">
<property name="id" value="100"/>
<property name="name">
<value>]]>value>
property>
bean>
<bean id="userDao" class="com.jt.dao.UserDaoImpl"/>
<bean id="userService" class="com.jt.service.UserServiceImpl" autowire="byName">
bean>
<bean id="userController" class="com.jt.controller.UserController" autowire="byType">
bean>
beans>
Spring为了简化xml配置方式,则研发注解模式.
Spring为了程序更加的严谨,通过不同的注解标识不同的层级 但是注解的功能一样
/**
*
* 如果需要修改beanId则手动添加value属性即可
*/
@Repository(value = "userDao")
public class UserDaoImpl implements UserDao{
@Override
public void addUser(User user) {
System.out.println("链接数据库执行insert into :"+user);
}
}
<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">
<bean id="user" class="com.jt.pojo.User">
<property name="id" value="100">property>
<property name="name">
<value>]]>value>
property>
bean>
<context:component-scan base-package="com.jt">context:component-scan>
<context:component-scan base-package="com.jt" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
context:component-scan>
<context:component-scan base-package="com.jt">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
context:component-scan>
beans>
上述的属性的注入在调用时 自动的封装了Set方法,所以Set方法可以省略不写
import com.jt.dao.UserDao;
import com.jt.pojo.User;
import org.springframework.stereotype.Service;
@Service
public class UserServiceImpl implements UserService{
@Autowired
private UserDao userDao;//基于Spring注入dao 面向接口编程
// public void setUserDao(UserDao userDao) {
// this.userDao = userDao;
//}
@Override
public void addUser(User user) {
String name = user.getName() + "加工数据";
user.setName(name);
userDao.addUser(user);
}
}
public class User {
//自动装配不能注入简单属性
private Integer id;
private String username;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
'}';
}
}
import com.jt.pojo.User;
public interface UserDao {
void addUser(User user);
}
import com.jt.pojo.User;
import org.springframework.stereotype.Repository;
@Repository
public class UserDaoImpl implements UserDao {
@Override
public void addUser(User user) {
System.out.println("新增用户的数据" + user);
}
}
import com.jt.pojo.User;
public interface UserService {
void addUser(User user);
}
import com.jt.dao.UserDao;
import com.jt.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserServiceImpl implements UserService {
@Autowired //根据类型进行注入 如需要根据属性名称注入需要添加@Qualifier注解————必须按照名称进行匹配
private UserDao userDao;
@Override
public void addUser(User user) {
userDao.addUser(user);
}
}
import com.jt.pojo.User;
import com.jt.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
@Controller
public class UserController {
@Autowired
private UserService userService;
public void addUser() {
User user = new User();
user.setId(101);
user.setUsername("王昭君|不知火舞");
userService.addUser(user);
}
}
<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.jt"/>
beans>
@Test
public void test01(){
ApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
UserController userController = context.getBean(UserController.class);
userController.addUser();
}
警告: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userController': Unsatisfied dependency expressed through field 'userService'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.jt.service.UserService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userController': Unsatisfied dependency expressed through field 'userService'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.jt.service.UserService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
随着软件技术发展,xml配置文件显得臃肿 不便于操作,所以Spring后期提出了配置类的思想
将所有的配置文件中的内容,写到java类中.
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration //标识我是一个配置类 相当于application.xml
@ComponentScan("com.jt") //如果注解中只有value属性 则可以省略
public class SpringConfig {
}
@Test
public void testAnno(){
ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
UserController userController = context.getBean(UserController.class);
userController.addUser();
}
Spring中规定 一个接口最好只有一个实现类.
import com.jt.pojo.User;
import com.jt.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
@Controller
public class UserController {
/**
* @Autowired: 首先根据属性的类型进行注入,
* 如果类型不能匹配,则根据属性的名称进行注入.
* 如果添加了@Qualifier("userServiceA") 则根据属性名称注入
* 如果名称注入失败,则报错返回.
*/
@Autowired
@Qualifier("userServiceB")
private UserService userService;
public void addUser() {
User user = new User();
user.setId(101);
user.setUsername("王昭君|不知火舞");
userService.addUser(user);
}
}
通过该注解,可以将业务数据实例化之后,交给Spring容器管理. 但是@Bean注解应该写到配置类中.
import com.jt.pojo.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration //标识我是一个配置类 相当于application.xml
@ComponentScan("com.jt") //如果注解中只有value属性 则可以省略
public class SpringConfig {
/*
1.Spring配置文件写法
2.执行@Bean的方法 将方法名称当做ID,返回值的对象当做value 直接保存到Map集合中
*/
@Bean
public User user(){
User user = new User();
user.setId(101);
user.setUsername("Spring容器");
return user;
}
}
import com.jt.pojo.User;
import com.jt.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
@Controller
public class UserController {
/**
* @Autowired: 首先根据属性的类型进行注入,
* 如果类型不能匹配,则根据属性的名称进行注入.
* 如果添加了@Qualifier("userServiceA") 则根据属性名称注入
* 如果名称注入失败,则报错返回.
*/
@Autowired
@Qualifier("userServiceA")
private UserService userService;
@Autowired
private User user; //从容器中动态获取
public void addUser() {
userService.addUser(user);
}
}
# 规则: properties文件
# 数据结构类型: k-v结构
# 存储数据类型: 只能保存String类型
# 加载时编码格式: 默认采用ISO-8859-1格式解析 中文必然乱码
user.id=1001
# Spring容器获取的当前计算机的名称 所以user.name慎用
# user.name=你好啊哈哈哈
user.username=鲁班七号
java @Value(123) 将123值赋值给Id @Value("${user.id}") 在Spring容器中查找key=user.id的数据.通过${}语法获取
import com.jt.pojo.User;
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 //标识我是一个配置类 相当于application.xml
@ComponentScan("com.jt") //如果注解中只有value属性 则可以省略
//@PropertySource 作用: 加载指定的pro配置文件 将数据保存到Spring容器中
//encoding:指定字符集编码格式
@PropertySource(value = "classpath:/user.properties",encoding = "UTF-8")
public class SpringConfig {
//定义对象属性 准备接收数据
//@Value(123) 将123值赋值给Id
//@Value("${user.id}") 在Spring容器中查找key=user.id的数据.通过${} 进行触发 @Value("${user.id}")
@Value("${user.id}")
private Integer id;
@Value("${user.username}")
private String username;
/*
1.Spring配置文件写法
2.执行@Bean的方法 将方法名称当做ID,返回值的对象当做value 直接保存到Map集合中
*/
@Bean
public User user(){
User user = new User();
user.setId(id);
user.setUsername(username);
return user;
}
}