package com.fafa.dao.impl;
import com.fafa.dao.UserDao;
import com.fafa.entity.User;
import org.springframework.stereotype.Repository;
//设置名字因为有两个dao实现类,service不知道找哪个
//注入spring的bean容器中
@Repository("DaoMysql")
public class DaoMysql implements UserDao {
@Override
public void findUserByID(User user) {
System.out.println("mysql按id查询");
}
@Override
public void addUser(User user) {
System.out.println("mysql添加用户");
}
}
package com.fafa.dao.impl;
import com.fafa.dao.UserDao;
import com.fafa.entity.User;
import org.springframework.stereotype.Repository;
//设置名字因为有两个dao实现类,service不知道找哪个
@Repository("DaoOracle")
public class DaoOracle implements UserDao {
@Override
public void findUserByID(User user) {
System.out.println("oracle按id查询");
}
@Override
public void addUser(User user) {
System.out.println("oracle添加用户");
}
}
package com.fafa.service.impl;
import com.fafa.dao.UserDao;
import com.fafa.entity.User;
import com.fafa.service.UserService;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
//注入spring的bean容器中
@Service
//必须提供Data 放bean容器中让ioc接管
@Data
public class UserServiceImple implements UserService {
//设置使用了哪个dao
//自动装配
@Resource(name = "DaoOracle")
private UserDao userDao;
@Override
public void findUserByID(User user) {
userDao.findUserByID(user);
}
@Override
public void addUser(User user) {
userDao.addUser(user);
}
}
package com.fafa.controller;
import com.fafa.entity.User;
import com.fafa.service.UserService;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
//注入spring的bean容器中
@Controller
//必须提供Data 放bean容器中让ioc接管
@Data
public class Usercontroller {
//自动装配
@Autowired
private UserService userService;
public void findUserByID(User user) {
userService.findUserByID(user);
}
public void addUser(User user) {
userService.addUser(user);
}
}
config
package com.fafa.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
//声明是个配置类
@Configuration
//作用就是根据定义的扫描路径,把符合扫描规则的类装配到spring容器中
@ComponentScan("com.fafa")
public class BeansConfig {
}
测试
package com.fafa.test;
import com.fafa.config.BeansConfig;
import com.fafa.controller.Usercontroller;
import com.fafa.entity.User;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringTest {
@Test
public void test01(){
//添加配置文件
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(BeansConfig.class);
//添加controller类
Usercontroller usercontroller = context.getBean(Usercontroller.class);
usercontroller.findUserByID(null);
}
}
第一种全注解方法加beans.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"
xmlns:context="http://www.springframework.org/schema/context"
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-4.3.xsd">
//在 xml 配置了这个标签后,spring 可以自动去扫描 base-pack 下面或者子包下面的 java 文件,如果扫描到有 @Component @Controller@Service 等这些注解的类,则把这些类注册为 bean
<context:component-scan base-package="com.fafa"/>
//开启这样就可以使用 @ Resource 、@ PostConstruct、@ PreDestroy、@PersistenceContext、@Autowired、@Required 等注解了,就可以实现自动注入 注册这 4 个 BeanPostProcessor 的作用,就是为了你的系统能够识别相应的注解。
<context:annotation-config/>
</beans>
测试
package com.fafa.test;
import com.fafa.controller.Usercontroller;
import com.fafa.entity.User;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringTest {
@Test
public void test01(){
//beans.xml文件
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
//controller类
Usercontroller bean = context.getBean(Usercontroller.class);
bean.findUserByID(new User(2,"dafa"));
}
}
导入包
4.0.0
com.fafa
spring02
1.0-SNAPSHOT
4.12
3.2.8
5.1.44
1.6.6
4.3.14.RELEASE
1.18.0
org.projectlombok
lombok
${lombok.version}
org.springframework
spring-context
${spring.version}
org.springframework
spring-context-support
${spring.version}
org.springframework
spring-core
${spring.version}
org.springframework
spring-beans
${spring.version}
org.springframework
spring-expression
${spring.version}
junit
junit
${junit.version}
test
org.slf4j
slf4j-log4j12
1.6.6
org.apache.maven.plugins
maven-compiler-plugin
3.2
1.8
UTF-8
package com.fafa.entity;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class User {
private Integer id;
private String name;
}
编写三层
1.dao层
package com.fafa.dao;
import com.fafa.entity.User;
public interface UserDao {
public void findUserByID(User user);
public void addUser(User user);
}
package com.fafa.dao.impl;
import com.fafa.dao.UserDao;
import com.fafa.entity.User;
public class DaoMysql implements UserDao {
@Override
public void findUserByID(User user) {
System.out.println("mysql按id查询");
}
@Override
public void addUser(User user) {
System.out.println("mysql添加用户");
}
}
package com.fafa.dao.impl;
import com.fafa.dao.UserDao;
import com.fafa.entity.User;
public class DaoOracle implements UserDao {
@Override
public void findUserByID(User user) {
System.out.println("oracle按id查询");
}
@Override
public void addUser(User user) {
System.out.println("oracle添加用户");
}
}
2.service层
package com.fafa.service;
import com.fafa.entity.User;
public interface UserService {
public void findUserByID(User user);
public void addUser(User user);
}
package com.fafa.service.impl;
import com.fafa.dao.UserDao;
import com.fafa.entity.User;
import com.fafa.service.UserService;
import lombok.Data;
@Data
public class UserServiceImple implements UserService {
private UserDao userDao;
@Override
public void findUserByID(User user) {
userDao.findUserByID(user);
}
@Override
public void addUser(User user) {
userDao.addUser(user);
}
}
3.controller层
package com.fafa.controller;
import com.fafa.entity.User;
import com.fafa.service.UserService;
import lombok.Data;
@Data
public class Usercontroller {
private UserService userService;
public void findUserByID(User user) {
userService.findUserByID(user);
}
public void addUser(User user) {
userService.addUser(user);
}
}
beans.xml
<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="DaoMysql" class="com.fafa.dao.impl.DaoMysql">bean>
<bean id="DaoOracle" class="com.fafa.dao.impl.DaoOracle">bean>
<bean id="userService" class="com.fafa.service.impl.UserServiceImple">
<property name="userDao" ref="DaoOracle">property>
bean>
<bean id="userController" class="com.fafa.controller.Usercontroller" >
<property name="userService" ref="userService">property>
bean>
beans>
测试
package com.fafa.test;
import com.fafa.controller.Usercontroller;
import com.fafa.entity.User;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringTest {
@Test
public void test01(){
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Usercontroller bean = context.getBean(Usercontroller.class);
bean.findUserByID(User.builder().id(1).name("fafa").build());
}
}