Spring | Home
package com.xiaolin.service.Impl;
import com.xiaolin.dao.UserDao;
import com.xiaolin.service.UserService;
public class UserServiceImpl implements UserService {
//该方法是BeanFactory去调用该方法 从容器中获取userDap设置到此处
public void setUserDao(UserDao userDao) {
System.out.println("该方法是BeanFactory去调用该方法 从容器中获取userDap设置到此处"+userDao);
}
}
package com.xiaolin.service;
public interface UserService {
}
public class BeanFactoryTest {
public static void main(String[] args) {
//创建工厂对象
DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
//创建一个读取器(xml文件)
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanFactory);
//读取器配置文件给工厂
reader.loadBeanDefinitions("beans.xml");
//根据id获取Bean实例对象
UserService userService = (UserService) beanFactory.getBean("userService");
// System.out.println(userService);
}
}
package com.xiaolin.dao.Impl;
import com.xiaolin.dao.UserDao;
public class UserDaoImpl implements UserDao {
}
package com.xiaolin.dao;
public interface UserDao {
}
public class BeanFactoryTest {
public static void main(String[] args) {
//创建工厂对象
DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
//创建一个读取器(xml文件)
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanFactory);
//读取器配置文件给工厂
reader.loadBeanDefinitions("beans.xml");
//根据id获取Bean实例对象
UserService userService = (UserService) beanFactory.getBean("userService");
// System.out.println(userService);
UserDao userDao = (UserDao) beanFactory.getBean("userDao");
// System.out.println(userDao);
}
}
package com.xiaolin.test;
import com.xiaolin.service.UserService;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class ApplicationContextTest {
public static void main(String[] args) {
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService = (UserService) applicationContext.getBean("userService");
System.out.println(userService);
}
}
当没有无参构造方法而使用有参构造方法时候,要在相关的配置文件中使用
,进行配置,name对应参数名,value对应相关的值
指定其他环境的情况下,默认环境都起作用
先在pom导入坐标
Object userDao1 =applicationContext.getBean("userDao1");
UserDao userDao11 = applicationContext.getBean("userDao1", UserDao.class);
UserDao bean = applicationContext.getBean(UserDao.class);
mysql
mysql-connector-java
5.1.49
com.alibaba
druid
1.1.23
beanDefinitionMap->bean工厂后处理器->循环map(实例化->bean后处理器->填充到单例池)
对Bean方法进行执行时间日志增强
public class TimeLogBeanPostProcessor implements BeanPostProcessor {
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
//使用动态代理对目标Bean进行增强,返回proxy对象,进而存储到单例池singletonObjects中
Object beanProxy = Proxy.newProxyInstance(
bean.getClass().getClassLoader(),
bean.getClass().getInterfaces(),
(proxy, method, args) -> {
//1、输出开始时间
System.out.println("方法:" + method.getName() + "-开始时间:" + new Date());
//2、执行目标方法
Object result = method.invoke(bean, args);
//3、输出结束时间
System.out.println("方法:" + method.getName() + "-结束时间:" + new Date());
return result;
}
);
return beanProxy;
}
}
55-Spring IoC容器实例化Bean整体流程图_哔哩哔哩_bilibili
public interface UserMapper {
List findAll();
}
mybatis-config.xml
MyBatisTest
public class MyBatisTest {
public static void main(String[] args) throws Exception {
InputStream in = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
SqlSessionFactory sqlSessionFactory = builder.build(in);
SqlSession sqlSession = sqlSessionFactory.openSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
List all = mapper.findAll();
for (User user : all) {
System.out.println(user);
}
}
}
org.springframework
spring-jdbc
5.2.13.RELEASE
org.mybatis
mybatis-spring
2.0.5
执行完SqlSessionFactoryBean后,里面调用了getObject然后产生SqlSessionFactory。
60-Spring整合第三方框架-MyBatis整合Spring-MapperScannerConfigurer_哔哩哔哩_bilibili
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis
jdbc.username=root
jdbc.password=root
62-Spring整合第三方框架-自定义命名空间解析原理_哔哩哔哩_bilibili
在@Component后面不加value值,默认id是该类名首字母小写
根据类型进行注入,如果同一个类型的Bean有多个,尝试根据名字进行二次匹配,匹配变不成功的则匹配失败
结合@Autowired一起使用,作用是根据名称注入相应的Bean
在非自定义属性上要@Autowird+@Qualifier
在自定义属性中添加参数名可以单独使用@Qualifier
不指定名称参数时,根据类型注入【@Autowired】,指定名称就根据名称注入【@Autowired+@Qualifier】
@Component
public class OtherBean {
@Bean("dataSource")
public DataSource dataSource(
@Value("${jdbc.driver}") String driverClassName,
@Qualifier("userDao2") UserDao userDao,
UserService userService
)
{
/*System.out.println(driverClassName);
System.out.println(userDao);
System.out.println(userService);*/
DruidDataSource dataSource = new DruidDataSource();
//设置4个基本参数 ...
return dataSource;
}
}
@Configuration //标注当前类是一个配置类(替代配置文件)+@Component-->替代了
//
@ComponentScan("com.itheima")//相当于包扫描
//
@PropertySource("classpath:jdbc.properties")//属性资源
//
@Import(OtherBean.class)
没有指定环境,在任何情况下都可以使用