通过无参构造方法来创建对象
@Data
public class Hello {
private String name;
public Hello(){
System.out.println("hello无参构造方法");
}
public void show(){
System.out.println("Hello,"+ name );
}
}
@Test
public void test(){
//解析beans.xml文件 , 生成管理相应的Bean对象
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
//getBean : 参数即为spring配置文件中bean的id .
Hello hello = (Hello) context.getBean("hello");
hello.show();
}
结果可以发现,在调用show方法之前,User对象已经通过无参构造初始化了!
通过有参构造方法来创建
@Data
@AllArgsConstructor
public class UserT {
private String name;
private int age;
public void show(){
System.out.println("name = " + name);
System.out.println("age = " + age);
}
}
beans.xml 有三种方式编写
第一种:根据index参数下标设置
<?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">
<!-- 第一种根据index参数下标设置 -->
<bean id="userT" class="com.yao.pojo.UserT">
<!-- index指构造方法 , 下标从0开始 -->
<constructor-arg index="0" value="kuangshen2"/>
<constructor-arg index="1" value="18"/>
</bean>
</beans>
第二种:根据参数名字设置
<!-- 第二种根据参数名字设置 -->
<bean id="userT" class="com.kuang.pojo.UserT">
<!-- name指参数名 -->
<constructor-arg name="name" value="kuangshen2"/>
<constructor-arg name="age" value="11"/>
</bean>
第三种:根据参数类型设置
<!-- 第三种根据参数类型设置 -->
<bean id="userT" class="com.yao.pojo.UserT">
<constructor-arg type="java.lang.String" value="kuangshen2"/>
<constructor-arg type="int" value="15"/>
</bean>
Test
@Test
public void testT(){
ApplicationContext context=new ClassPathXmlApplicationContext("beans.xml");
UserT user= (UserT) context.getBean("userT");
user.show();
}
输出:
结论:
在配置文件加载的时候。其中管理的对象都已经初始化了!
Spring配置
Alias(没啥用,因为有Bean的配置)
<?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="userT" class="com.yao.pojo.UserT">
<constructor-arg type="java.lang.String" value="kuangshen2"/>
<constructor-arg type="int" value="15"/>
</bean>
<!--设置别名:在获取Bean的时候可以使用别名获取-->
<alias name="userT" alias="T"/>
</beans>
@Test
public void testT(){
ApplicationContext context=new ClassPathXmlApplicationContext("beans.xml");
UserT user= (UserT) context.getBean("T");
user.show();
}
Bean的配置
- id是bean的标识符,要唯一,如果没有配置id,name就是默认标识符
- 如果配置id,又配置了name,那么name是别名
- name可以设置多个别名,可以用逗号,分号,空格隔开
- 如果不配置id和name,可以根据applicationContext.getBean(.class)获取对象
<!--bean就是java对象,由Spring创建和管理-->
<!--
id 是bean的标识符,要唯一,如果没有配置id,name就是默认标识符
如果配置id,又配置了name,那么name是别名
name可以设置多个别名,可以用逗号,分号,空格隔开
如果不配置id和name,可以根据applicationContext.getBean(.class)获取对象;
class是bean的全限定名=包名+类名
-->
<bean id="userT" name="u,uu uuu;uuuu" class="com.yao.pojo.UserT">
<property name="name" value="kuangshen2"/>
<property name="age" value="15"/>
</bean>
import
团队的合作通过import来实现
Test
@Test
public void testT(){
// ApplicationContext context=new ClassPathXmlApplicationContext("beans.xml","beans2.xml");
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
UserT user= (UserT) context.getBean("uuuu");
user.show();
}
applicationContext.xml下import
<?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">
<import resource="beans.xml"/>
<import resource="beans2.xml"/>
<import resource="beans3.xml"/>
<import resource="beans4.xml"/>
</beans>
重复的会合并