Spring 基于XML开发 bean 配置 连接池
目录
约束
spring约束需要在spring官网复制 为了省去麻烦 这里直接给出网址Core Technologies
方框中的就是约束 把它复制下来放到xml文件中 这里只是简单的约束 下面是全部约束
-
<beans xmlns="http://www.springframework.org/schema/beans"
-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-
xmlns:p="http://www.springframework.org/schema/p"
-
xmlns:context="http://www.springframework.org/schema/context"
-
xmlns:aop="http://www.springframework.org/schema/aop"
-
xmlns:mvc="http://www.springframework.org/schema/mvc"
-
xmlns:tx="http://www.springframework.org/schema/tx"
-
xsi:schemaLocation="http://www.springframework.org/schema/beans
-
http://www.springframework.org/schema/beans/spring-beans.xsd
-
http://www.springframework.org/schema/mvc
-
http://www.springframework.org/schema/mvc/spring-mvc.xsd
-
http://www.springframework.org/schema/aop
-
http://www.springframework.org/schema/aop/spring-aop.xsd
-
http://www.springframework.org/schema/context
-
http://www.springframework.org/schema/context/spring-context.xsd
-
http://www.springframework.org/schema/tx
-
http://www.springframework.org/schema/tx/spring-tx.xsd">
bean 存入spring容器
bean标签:用于配置让spring创建对象 并且存入ioc容器中
id:对象的唯一标识
class:指定类的全限定类名
例如
<bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl">
当对象中有属性的时候必须 要有set方法 xml中使用property给对象属性赋值
property标签:给对象属性赋值
name:对象属性名
ref:引用bean标签创建的对象 赋值给对象的属性
-
<bean id = "userDao" class="com.czxy.demo02.dao.impl.UserDaoImpl"></bean>
-
-
<bean id = "userService" class="com.czxy.demo02.service.Impl.UserServiceImpl">
-
<property name="userDaoimpl" ref="userDao"></property>
-
</bean>
整合Junit
对应整合Junit只是加载配置文件有些不同 以前加载配置文件@ContextConfiguration的属性是
classes 现在xml加载配置文件@ContextConfiguration属性是locations 是个数组 指定xml配置文件
-
@RunWith(SpringRunner.class)
-
@ContextConfiguration(locations = {"classpath:demo03.xml"})
-
public class TestA {
-
-
@Resource(name = "dataSource")
-
private DataSource dataSource;
-
-
-
@Test
-
public void test01(){
-
System.out.println(dataSource);
-
}
-
}
手动获取Spring容器
BeanFactory是所有类的顶层接口
ClassPathXmlApplicationContext:从类路径下加载配置文件【使用这种】
FileSystemXmlApplicationContext:从磁盘路径下加载配置文件
使用getBean获取对象
-
public class TestB {
-
public static void main(String[] args) {
-
BeanFactory beanFactory = new ClassPathXmlApplicationContext("demo01.xml");
-
User user = (User)beanFactory.getBean("user");
-
user.dao();
-
}
-
}
xml配置连接池
使用context:property-placeholder加载属性配置文件
最后创建Druid对象 依次注入属性 放入IOC容器
-
<?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:p="http://www.springframework.org/schema/p"
-
xmlns:context="http://www.springframework.org/schema/context"
-
xmlns:aop="http://www.springframework.org/schema/aop"
-
xmlns:mvc="http://www.springframework.org/schema/mvc"
-
xmlns:tx="http://www.springframework.org/schema/tx"
-
xsi:schemaLocation="http://www.springframework.org/schema/beans
-
http://www.springframework.org/schema/beans/spring-beans.xsd
-
http://www.springframework.org/schema/mvc
-
http://www.springframework.org/schema/mvc/spring-mvc.xsd
-
http://www.springframework.org/schema/aop
-
http://www.springframework.org/schema/aop/spring-aop.xsd
-
http://www.springframework.org/schema/context
-
http://www.springframework.org/schema/context/spring-context.xsd
-
http://www.springframework.org/schema/tx
-
http://www.springframework.org/schema/tx/spring-tx.xsd">
-
-
<context:property-placeholder location = "classpath:db.properties"></context:property-placeholder>
-
-
<bean id = "dataSource" class="com.alibaba.druid.pool.DruidDataSource">
-
<property name="driverClassName" value="${jdbc.driver}"></property>
-
<property name="url" value="${jdbc.url}"></property>
-
<property name="username" value="${jdbc.username}"></property>
-
<property name="password" value="${jdbc.password}"></property>
-
</bean>
-
</beans>
文章来源: blog.csdn.net,作者:陶然同学,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/weixin_45481821/article/details/121752566
- 点赞
- 收藏
- 关注作者
评论(0)