文章目录
手动装配
dao层
- com.jepcc.demo.dao.UserDao
package com.jepcc.demo.dao;
public interface UserDao {
void getUser();
}
- com.jepcc.demo.dao.impl.MysqlUserDaoImpl
package com.jepcc.demo.dao.impl;
import com.jepcc.demo.dao.UserDao;
public class MysqlUserDaoImpl implements UserDao {
@Override
public void getUser() {
System.out.println("Mysql user dao");
}
}
- com.jepcc.demo.dao.impl.OrcaleUserDaoImpl
package com.jepcc.demo.dao.impl;
import com.jepcc.demo.dao.UserDao;
public class OrcaleUserDaoImpl implements UserDao {
@Override
public void getUser() {
System.out.println("Orcale user dao");
}
}
service层
- com.jepcc.demo.service.UserService
package com.jepcc.demo.service;
public interface UserService {
void getUser();
}
- com.jepcc.demo.service.impl.UserServiceImpl
package com.jepcc.demo.service.impl;
import com.jepcc.demo.dao.UserDao;
import com.jepcc.demo.service.UserService;
public class UserServiceImpl implements UserService {
private UserDao userDao;
public void setUserDao(UserDao userDao){
this.userDao = userDao;
}
@Override
public void getUser() {
userDao.getUser();
}
}
测试类Test
- com.jepcc.demo.test.Test
package com.jepcc.demo.test;
import com.jepcc.demo.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("config.xml");
UserService service = (UserService) context.getBean("service");
service.getUser();
}
}
bean配置文件config.xml
<?xml version="1.0" encoding="UTF-8"?>
<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="mysqlUserDao" class="com.jepcc.demo.dao.impl.MysqlUserDaoImpl"></bean>
<bean id="service" class="com.jepcc.demo.service.impl.UserServiceImpl">
<property name="userDao" ref="mysqlUserDao"></property>
</bean>
</beans>
测试验证
自动装配
bean的自动装配,是为了简化spring配置。
局部装配
通过bean的autowire
属性来指定装配方式,autowire
属性有四个值,分别是
no
byName
byType
constructor
no,默认值,即不使用自动装配
byName,通过名称实现自动装配
<?xml version="1.0" encoding="UTF-8"?>
<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="userDao" class="com.jepcc.demo.dao.impl.MysqlUserDaoImpl"></bean>
<bean id="service" class="com.jepcc.demo.service.impl.UserServiceImpl" autowire="byName">
</bean>
</beans>
byType,通过类型实现自动装配
<?xml version="1.0" encoding="UTF-8"?>
<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 class="com.jepcc.demo.dao.impl.MysqlUserDaoImpl"></bean>
<bean id="service" class="com.jepcc.demo.service.impl.UserServiceImpl" autowire="byType">
</bean>
</beans>
constructor,类似于byType,即通过构造器实例化bean对象。
- com.jepcc.demo.service.impl.UserServiceImpl
package com.jepcc.demo.service.impl;
import com.jepcc.demo.dao.UserDao;
import com.jepcc.demo.service.UserService;
public class UserServiceImpl implements UserService {
private UserDao userDao;
public UserServiceImpl(UserDao userDao) {
this.userDao = userDao;
}
@Override
public void getUser() {
userDao.getUser();
}
}
- config.xml
<?xml version="1.0" encoding="UTF-8"?>
<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 class="com.jepcc.demo.dao.impl.MysqlUserDaoImpl"></bean>
<bean id="service" class="com.jepcc.demo.service.impl.UserServiceImpl" autowire="constructor">
</bean>
</beans>
全局装配
通过头部的default-autowire
属性来指定自动装配方式。
<?xml version="1.0" encoding="UTF-8"?>
<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" default-autowire="byName">
<bean id="userDao" class="com.jepcc.demo.dao.impl.MysqlUserDaoImpl"></bean>
<bean id="service" class="com.jepcc.demo.service.impl.UserServiceImpl">
</bean>
</beans>