大家可以关注作者的账号,关注从零开始学Spring笔记文集。也可以根据目录前往作者的博客园博客进行学习。本片文件将基于黑马程序员就业班视频进行学习以及资料的分享,并记录笔记和自己的看法。欢迎大家一起学习和讨论。
【从零开始学Spring笔记】Spring学习路线
第一步:Window->Preference->xml catlog->User Specified Entries->add的顺序进入第二步界面
第二步:按照图示步骤修改
操作1:
找到之前配置文件中的约束最后一行网址,赋值
.xsd
操作2:安装路径
spring-framework-4.2.4.RELEASEschemabeans
先解压jar包,找到这个,最后选择4.2版本
操作3:选择schema location
第三步:点击OK,返回。点击Apply and Close
配置成功后,在编写xml文件时,就有相应的提示了。
id :使用了约束中的唯一约束。里面不能出现特殊字符的。
name :没有使用约束中的唯一约束(理论上可以出现重复的,但是实际开发不能出现的)。里面可以出现特殊字符。
Spring和Struts1框架整合的时候,Struts1名称前必须使用 “/”,所以使用Spring整合时使用name,例如
init-method :Bean被初始化的时候执行的方法
destroy-method :Bean被销毁的时候执行的方法(Bean是单例创建,工厂关闭)
在配置文件中输入init-method="setup" destroy-method="destroy"
即可,在class指定的类中建立setup()和destory()方法,setup()在该类被实例化时调用,destory()在该类被销毁的时候调用。方法名可以自定义
scope | Bean的作用范围 |
---|---|
singleton | 默认的,Spring会采用单例模式创建这个对象。 |
prototype | 多例模式。(Struts2和Spring整合一定会用到,Struts2是多例) |
request | 应用在web项目中,Spring创建这个类以后,将这个类存入到request范围中。 |
session | 应用在web项目中,Spring创建这个类以后,将这个类存入到session范围中。 |
globalsession | 应用在web项目中,必须在porlet环境(登录百度,再登录百度的子网站,例如百度地图,就不需要登陆了)下使用。但是如果没有这种环境,相对于session。 |
在配置文件中输入scope ="singleton"
即可,实现作用范围的配置,默认时单例。单例即只被实例一次,无论调用多少次,命名不同也是同一个实例。多例即调用一次实例一次,每个实例均不同。
Bean已经都交给Spring管理,Spring创建这些类的时候,有几种方式:
编写类
ust.spring.demo04;public class Bean1 {public Bean1() {super();System.out.println("Bean1的无参构造方法执行了...");}}
编写配置文件
<!-- 无参构造 --><bean id="1" class=ust.spring.demo04.Bean1"></bean>
编写测试方法
private static void mothed1() {ApplicationContext applicationContext = new ClassPathXmlApplicationContext(l");Bean1 b = (Bean1) Bean("1");}
输出结果
创建类并且编写Bean2的静态工厂
ust.spring.demo04;public class Bean2 {}
-------------------------------------------------------------------------------------------
ust.spring.demo04;public class Bean2Factory {public static Bean2 createBean2() {System.out.println("Bean2Factory中的方法执行了...");return new Bean2();}
}
编写配置文件
<!--静态工厂实例化--> <bean id="2" class=ust.spring.demo04.Bean2Factory" factory-method="createBean2"></bean>
编写测试方法
private static void mothed2() {ApplicationContext applicationContext = new ClassPathXmlApplicationContext(l");Bean2 b2 = (Bean2) Bean("2");System.out.println(b2);}
输出结果
创建类并且编写Bean2的实例化工厂
ust.spring.demo04;public class Bean3 {}
-------------------------------------------------------------------------------------------
ust.spring.demo04;public class Bean3Factory {public Bean3 createBean3() {System.out.println("Bean3Factory中的方法执行了...");return new Bean3();}}
编写配置文件
<!--实例工厂实例化 --><bean id="3" class=ust.spring.demo04.Bean3Factory" ></bean><bean id="4" factory-bean="3" factory-method="createBean3"></bean>
编写测试方法
public static void mothed3(){ApplicationContext applicationContext = new ClassPathXmlApplicationContext(l");Bean3 b3 = (Bean3) Bean("4");System.out.println(b3);}
输出结果
总结:因为ApplicationContext在调用时就会实例化,所静态方法可以直接通过工厂方法调用,因为静态方法是不属于对象的。而普通实例化方法,必须有对象才能调用,所有在配置文件时,必须先创建一个对象才能实例化。
1.构造方法的方式的属性注入
2.Set方法的方式的属性注入
示例
<!-- 构造方法的属性注入 --><bean id="car" class=ust.spring.demo03.Car"><constructor-arg name="name" value="宝马"></constructor-arg><constructor-arg name="price" value="80w"></constructor-arg></bean><!-- Set方法的属性注入 --><bean id="car2" class=ust.spring.demo03.Car2"><property name="name" value="奔驰"></property><property name="price" value="100w"></property></bean><!-- Set方法设置对象类型的属性 --><bean id="employee" class=ust.spring.demo03.Employee"><property name="name" value="奔驰"></property><property name="car2" ref="car2"></property></bean>
输出结果
总结
构造方法用constructor-arg,set方法用property。普通类型用value,其他类型用ref。
3.p名称空间的属性注入(Spring2.5以后)
通过引入p名称空间完成属性的注入:
写法:
普通属性 p:属性名=”值”
对象属性 p:属性名-ref=”值”
引入p名称空间,如图所示加入下面这行代码
xmlns:p=""
示例,将上例修改
<!-- Set方法的属性注入 --><bean id="car2" class=ust.spring.demo03.Car2" p:name = "路虎" p:price = "50w"><!-- <property name="name" value="奔驰"></property><property name="price" value="100w"></property> --></bean><!-- Set方法设置对象类型的属性 --><bean id="employee" class=ust.spring.demo03.Employee" p:name = "王总" p:car2-ref = "car2"><!-- <property name="name" value="奔驰"></property><property name="car2" ref="car2"></property>--></bean>
输出结果
4.SpEL的属性注入(Spring3.0以后)
SpEL:Spring Expression Language,Spring的表达式语言。
语法:#{SpEL}
示例,将上例修改
<bean id="car2" class=ust.spring.demo03.Car2"><property name="name" value="#{'凯迪拉克'}"></property><property name="price" value="#{'25w'}"></property></bean><bean id="employee" class=ust.spring.demo03.Employee"><property name="name" value="#{'李总'}"></property><property name="car2" value="#{car2}"></property></bean>
输出结果
总结SpEL可以输出更多内容,包括一些类,以及方法的调用和一些计算。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""xmlns:p=""xmlns:xsi=""xsi:schemaLocation="://www.springframework/schema/beans/spring-beans.xsd"><bean id="collectionBean" class=ust.spring.demo05.CollectionBean"><property name="arrs"><array><value>刘备</value><value>张飞</value><value>关羽</value></array></property><property name="list"><list><value>曹操</value><value>曹丕</value><value>曹植</value></list></property><property name="set"><set><value>孙坚</value><value>孙策</value><value>孙权</value></set></property><property name="map"><map><entry key="诸葛亮" value="黄月英"></entry><entry key="周瑜" value="小乔"></entry><entry key="吕布" value="貂蝉"></entry></map></property></bean></beans>
输出结果
?ArrayList类的注入问题
1.在加载配置文件的时候,加载多个ApplicationContext applicationContext = new ClassPathXmlApplicationContext(l",l");
2.在一个配置文件中引入多个配置文件<import resource= l"/>
本文发布于:2024-01-29 10:55:17,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170649692214781.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |