1. Spring程序
1.1 helloSpring
- 配置xml文档Bean.xml:
这里的 id=“hello” class=“com.wu.pojo.Hello” 就相当于创建一个对象
Hello hello = new Hello(); id是变量名,class是变量类型。
~~ ~~
property标签内是在为str变量设置值,相当于实体类内的set方法(如果实体类
中的setStr方法被删除,那么这句就会报错。)
<?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="hello" class="com.wu.pojo.Hello">
<property name="str" value="hello"/>
</bean>
</beans>
- 编写一个实体类:
public class Hello {
private String str;
public String getStr() {
return str;
}
public void setStr(String str) {
this.str = str;
}
@Override
public String toString() {
return "hello{" +
"str='" + str + '\'' +
'}';
}
}
- 测试:
org.springframework.context.ApplicationContext: 接口代表 Spring IoC 容器,并负责实例化,配置和组装 Bean。容器通过读取配置元数据来获取有关要实例化,配置和组装哪些对象的指令。配置元数据以 XML,Java 注解或 Java 代码表示。它使您能够表达组成应用程序的对象以及这些对象之间的丰富相互依赖关系。
new ClassPathXmlApplicationContext():用来加载配置文件
通过 getBean()来获得配置文件中的bean以达到获取对象的目的。
public class Mytest {
@Test
public void test(){
ApplicationContext context = new ClassPathXmlApplicationContext("Bean.xml");
Hello hello = (Hello)context.getBean("hello");
System.out.println(hello.toString());
}
}
2. 优化上次写的程序
<?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.wu.dao.userDaoImpl"/>
<bean id="mysqldao" class="com.wu.dao.mysqlDaoImpl"/>
<bean id="userservice" class="com.wu.service.userServiceImpl">
<property name="userDao" ref="userdao"/>
</bean>
</beans>
ref:表示对象,value:表示值(通过ref的值来改变选择的实体类)
property标签内对应的是:
private userDao userDao;
public void setUserDao(com.wu.dao.userDao userDao) {
this.userDao = userDao;
}
测试:
@Test
public void test2() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("Bean.xml");
userServiceImpl userservice = (userServiceImpl) applicationContext.getBean("userservice");
userservice.getuser();
}
这样我们可以通过改变Bean.xml内的配置来选择使用哪个实现类。
2. IOC创建对象方式
创建一个类的对象就是在初始化此类的构造方法,当我们写一个有参的构造方法时(此时系统不会再隐式的创建空参的构造方法),会发现在Bean.xml中会报错。
所以构造方法是IOC创建对象方式的关键。
2.1 有参构造方法的Bean文件写法
- 通过寻找下标
<bean id="user" class="com.wu.pojo.user">
<constructor-arg index="0" value="lll"/>
</bean>
- 通过参数类型
<bean id="user" class="com.wu.pojo.user">
<constructor-arg type="java.lang.String" value="lll"/>
</bean>
- 直接通过变量名设置
<bean id="user" class="com.wu.pojo.user">
<constructor-arg name="name" value="lll"/>
</bean>
注:同时不管是否会被调用,在获得容器(ApplicationContext context)的同时Bean内设置的对象都会被创建,所有的对象都是被加载好了的。
3. Spring配置说明
就是beans标签内的子标签性质:
- alias 别名 :
<bean id="student" class="com.wu.pojo.student">
<property name="name" value="1"/>
</bean>
<alias name="student" alias="student2"/>
注:虽然起了别名但是原来的名字还是可以继续用。
-
bean:
id:标识单个 bean 定义的字符串,是唯一标识。
class:定义 bean 的类型并使用完全限定的类名。
name:与id类似,同时也是别名,但它可以起多个别名,不同的别名之间用分割符隔开。 -
import:
在多人合作的项目中会用到,创建一个ApplicationContext.xml,使用import将大家的Bean文件导入,这样就可以使用不同的bean了。
<import resource="Bean.xml" />