public class HelloSpring {
@Setter //必须有set属性
private String name;
public void say() {
System.out.println("hello---" + name);
}
}
@Test
//把创建的对象交给spring框架管理
public void test1() throws Exception {
//1读取资源文件
Resource resource = new ClassPathResource("/applicationContext.xml");
//2创建spring容器对象
BeanFactory bf = new XmlBeanFactory(resource);
//3从srping容器中获得指定名称对象
HelloSpring hello = bf.getBean("helloSpring", HelloSpring.class);
<?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">
<beans>
<bean name="helloSpring" class="全限定类名">
<property name="需要注入的属性名称" value="注入值" />
</bean>
</beans>
<import resource="classpath:bin目录文件路径"/>
@RunWith(SpringJUnit4ClassRunner.class)表示先启动Spring容器,把junit运行在Spring容器中
@ContextConfiguration("classpath:applicationContext.xml"):表示从哪里加载资源文件
public class HelloWorldTest {
@Autowired :表示自动装配
private BeanFactory factory;
@Test
public void testSpringTest() throws Exception {
HelloWorld helloWorld = factory.getBean("springTest", HelloWorld.class);
helloWorld.sayHello();
}
}
//针对于当前xml中所有的bean:是否需要延迟初始化. <bean lazy-init="default | false | true"> //针对于指定的bean:是否需要延迟初始化. <beans default-lazy-init="default | false | true">
<bean id="" class="" scope="作用域"/>
singleton: 单例 ,在Spring IoC容器中仅存在一个Bean实例 (默认的scope)
prototype: 多例 ,每次从容器中调用Bean时,都返回一个新的实例,即每次调用getBean()时 ,相当于执行new XxxBean():不会在容器启动时创建对象
request: 用于web开发,将Bean放入request范围 ,request.setAttribute("xxx") , 在同一个request 获得同一个Bean
session: 用于web开发,将Bean 放入Session范围,在同一个Session 获得同一个Bean
globalSession: 一般用于Porlet应用环境 , 分布式系统存在全局session概念(单点登录),如果不是porlet环境,globalSession 等同于Session
对于Struts2中的Action使用prototype类型,其他使用singleton
<bean />元素的:autowire属性 <bean id="somebean" class="SomeBean全限定名" autowire="byType"/>
//实体类
@Setter
public class Employee {
private String name;
}
//测试类
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class EmployeeTest {
@Autowired
private ApplicationContext ctx;
@Test
public void test1() throws Exception {
Employee bean = ctx.getBean("employee", Employee.class);
System.out.println(bean);
}
}
//application配置
<bean id="employee" class="com.***.Employee">
<property name="name" value="zoe" />
</bean>
public class Employee {
private String name;
public Employee(String name) {
this.name = name;
}
}
//application配置
<bean id="employee" class="com.***.Employee">
<constructor-arg name="name" value="zoe" />
</bean>
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class PropertyPlaceHolderTest {
@Autowired
private DataSource ds ;
@Test
public void testLink() throws Exception {
Connection conn = ds.getConnection();
String sql = "select * from user";
PreparedStatement ps = conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
}
}
<?xml version="1.0" encoding="UTF-8"?>
<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
">
<!-- 加载 properties -->
<!-- system-properties-mode="NEVER" 不使用系统默认属性 -->
<context:property-placeholder location="classpath:db.properties"
system-properties-mode="NEVER" />
<!-- 配置连接池 -->
<bean id="ds" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbd.driverClassName}" />
<property name="url" value="${jdbd.url}" />
<property name="jdbd.username" value="${username}" />
<property name="jdbd.password" value="${password}" />
</bean>
</beans>
jdbd.driverClassName=com.mysql.jdbc.Driver jdbd.url=jdbc:mysql:///springdemo jdbd.username=root jdbd.password=admin
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有