- 4.IOC创建对象的方式
- 5.Spring配置
- 别名
- Bean的配置
- import
- DI依赖注入
- 构造器注入
- Set方式注入!!!!
- 拓展方式注入
- Bean的作用域
- 好迷茫啊
-
使用无参构造创建对象,默认!
-
假设我们要使用有参构造创建对象
-
下标赋值
- 类型赋值:不建议使用
- 参数名
-
总结:在配置文件加载的时候,容器中管理的对象就已经初始化了
User
public class User {
private String name;
//没有无参构造,报错
public User(String name){
this.name = name;
}
//省略getter,setter
public void show(){
System.out.println("name:"+name);
}
}
applicationContext.xml配置
测试MyTest
import net.cqwu.pojo.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
public static void main(String[] args) {
//Spring容器,类似于婚介网站
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
User user = (User) context.getBean("user");
User user2 = (User) context.getBean("user");
user.show();
System.out.println(user == user2);
}
}
5.Spring配置
别名
Bean的配置
UserT user = (UserT) context.getBean("user2");
user.show();
import
团队开发时,将多个配置文件导入合并为一个
DI依赖注入 构造器注入
Set方式注入!!!
- 依赖注入:set注入
- 依赖:bean对象的创建依赖于容器
- 注入:bean对象中的所有属性,由容器来注入
【环境搭建】
- 复杂类型
public class Address {
private String address;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Address{" +
"address='" + address + ''' +
'}';
}
}
- 真实测试对象
public class Student {
private String name;
private Address address;
private String[] books;
private List hobbys;
private Map card;
private Set game;
private String wife;//空指针
private Properties info;
}
//get set toString省略
- applicationContext.xml
西游记 红楼梦 三国 水浒
魔方 唱歌 听歌 lol bob coc 4029 feliks 软件 admin 123456
- MyTest
public class MyTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Student student = (Student) context.getBean("student");
System.out.println(student.toString());
}
}
拓展方式注入
详细解释见官方文档中的
XML Shortcut with the p-namespace
XML Shortcut with the c-namespace
对象
public class User {
private String name;
private int age;
//加上无参 和 有参构造器,才能使用c命名注入
public User() {
}
public User(String name, int age) {
this.name = name;
this.age = age;
}
}//省略getter setter toString
测试类
@Test
public void test2() {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
User user = context.getBean("user2",User.class);
System.out.println(user);
}
beans.xml
注意:p 和 c命名空间注入不能直接使用,需要导入xml约束
xmlns:p="http://www.springframework.org/schema/p" xmlns:c="http://www.springframework.org/schema/c"Bean的作用域
官方文档:
| Scope | Description |
|---|---|
| singleton | (Default) Scopes a single bean definition to a single object instance for each Spring IoC container. |
| prototype | Scopes a single bean definition to any number of object instances. |
| request | Scopes a single bean definition to the lifecycle of a single HTTP request. That is, each HTTP request has its own instance of a bean created off the back of a single bean definition. only valid in the context of a web-aware Spring ApplicationContext. |
| session | Scopes a single bean definition to the lifecycle of an HTTP Session. only valid in the context of a web-aware Spring ApplicationContext. |
| application | Scopes a single bean definition to the lifecycle of a ServletContext. only valid in the context of a web-aware Spring ApplicationContext. |
| websocket | Scopes a single bean definition to the lifecycle of a WebSocket. only valid in the context of a web-aware Spring ApplicationContext. |
- 单例模式(Spring默认机制)
测试
User user = context.getBean("user2",User.class);
User user2 = context.getBean("user2",User.class);
System.out.println(user==user2);//true
- 原型模式:每次从容器中get的时候,都会产生一个新对象
测试
User user = context.getBean("user2",User.class);
User user2 = context.getBean("user2",User.class);
System.out.println(user==user2);//false
request session application只能在web开发中使用到
好迷茫啊就当是上天都看不下去了,派了个人来让我再短暂的体验了一把温暖吧,我现在不知道该干嘛,学也学不进去,这些突如其来的人和事搞得我措手不及。看似是我在布局,实则自己变成了棋子。棋局已成,我便是弃子?



