2020-03-15spring框架2
spring框架的相关内容(续)
Bean的作用范围的配置
scope :Bean的作用范围
singleton :默认的,spring会采用单例模式创立这个对象
prototype :多例模式
request :应用在web项目中,Spring创立这个类以后,将这个类存入到request范围中
session :应用在web项目中,Spring创立这个类以后,将这个类存入到session范围中
globalsession:应用在web项目中,必需在porlet环境下使用,但是假如没有这种环境,相当于session
单例模式下:一个类不论在测试类中实例化几次,地址都是一样的,都是一个类
多例模式下:一个类实例化几次就会创立几个对象,地址不同,假如在类中直接new则默认多例
bean的生命周期
1.执行bean构造器;
2.为bean注入属性;
3.调用<bean init-method=”init”/>初始化;
4.执行程序业务逻辑;
5.调用<bean destroy-method=”destory”/>进行销毁
PS:以上流程只在单例模式执行,多例模式只负责创立,其余一概不论
使用静态方法创立对象
新建xml文件
clipboard.png
<!-- 使用静态方法创立对象 --><bean class="com.testSpring.Car" id="car1" factory-method="staticInstance"></bean>使用实例工厂创立对象
需要新建一个类作为实例工厂
public class InstanceFactory { public Car Instance() { System.out.println("实例工厂"); return new Car(); }}xml文件中:
<!-- 使用实例方法创立 --><bean class="com.testSpring.InstanceFactory" id="instanceFactory"></bean><bean class="com.zut.testSpring.Car" id="car3" factory-bean="instanceFactory" factory-method="Instance"></bean>Spring属性注入的方式
构造方法方式
实体类:
private String s; public AddUsersController(String s) { this.s=s; } public String toString() { return ("["+s+"]"); }xml文件:
<bean class="com.testSpring.AddUsersController" id="userController" > <constructor-arg name="s" value="dfh"></constructor-arg> //这一句也可以写成:<constructor-arg index="0" value="dfh"></constructor-arg></bean>测试类:
ApplicationContext context=new ClassPathXmlApplicationContext("spring-config.xml");AddUsersController userController=context.getBean(AddUsersController.class);System.out.println(userController);须有有参构造
简写(c标签)
//加上引用xmlns:c="http://www.springframework.org/schema/c"<!-- 利用c标签简写 --> <bean class="" id="" c:name="" c:age=""></bean>set方法的注入
<bean class="com.testSpring.Car" id="car" > <property name="name" value="zhangsan"></property> <property name="price" value="10000"></property></bean><!-- value是设置普通类型的值,ref是用来设置其余的类的id或者name --><property name="car" ref="car"></property>//ref里的car是来源于上一个bean里的实例的idconstructor-arg:通过构造函数注入。 所以肯定要有构造函数
property:通过setter对应的方法注入。所以肯定要有set函数
P标签注入(set简写)
写法:
普通属性 p:属性名=”值”
对象属性 p:属性名-ref=”值”
<bean class="com.testSpring.Car" id="car" p:name="zhangsan" p:price="30000"></bean><bean class="com.testSpring.Employee" id="employee" p:name="zhangsan" p:car-ref="car"></bean>SPEL的属性注入
<!--SPEL的属性注入 --> <bean class="com.testSpring.Car" id="car" > <property name="name" value="#{'abc'}"></property> </bean> <bean class="com.testSpring.AddUsersController" id="userController"> <property name="s" value="#{car.name}"></property> </bean>**实体类中需要set数组
假如类的属性为spring[]数组
<bean class="com.testSpring.Car" id="car" > <property name="str" > <list> <value>av</value> <value>bc</value> </list> </property></bean> //假如装的是普通类型的话为value,假如为对象类型的话为ref //list同上,数组情况下list标签也可以改为array注入Set<String>和Map<String,String>
<property name="set" > <set> <value>av</value> <value>bc</value> </set> </property> //假如为对象类型则将value改为ref <property name="map" > <map> <entry key="a" value="b"></entry> </map> </property> //假如key和value为对象类型的,则改为key-ref和value-ref注解形式实现IOC
首先要在xml文件中勾选spring-context相关内容
clipboard.png
xml中会自动增加以下语句
xmlns:context="http://www.springframework.org/schema/context" 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">而后写入应用上下文扫描
<!-- 应用上下文扫描 --> <context:component-scan base-package="com.zut.testSpring"></context:component-scan>该语句会自动扫描pakage中所有带@Component的类
@Component注解
组件
通过注解进行bean实例化。
注解后可以不加括号,默认名称为首字母小写
有三个衍生注解:
@Controller -> web层
@Service -> service层
@Repository -> dao层
注解后加括号可以指定名称
@Component("c")@Value注解
普通属性,通过注解进行属性注入,可以不要set方法
@Value("aaabc")private String name;@Autowired注解
对象类型,自动按类型注入注入,相当于ref
自动寻觅复合要求的类进行注入,但是类前要有@Component注解
假如想要按名称注入,需与@Qualifier联用,但次名称需与被注入的类上的@Component所指定的名称相同。
当有多个符合要求的类时,会出现错误
@Autowired@Qualifier("c")private Car car;@Resource注解
默认按照bean的名称进行注入,要求指定注入名称
@Resource(name="userService")private UserService userService;@Scope注解
指定当前bean的作用范围 单例 singlton ,多例 prototype 等,前提是肯定要有上下文扫描
在类前标志
@PostConstruct和@PreDestory注解
@PostConstruct:相当于bean标签里配置的init-method,标志在类里的初始化方法前
@PostDestory:相当于bean标签里配置的destory-method,标志在类里的销毁方法前
Bean任意场景下均能使用,结构清晰,维护方便。注解开发方便,但是注解有些地方用不了,比方说这个类不是自己提供的。
整合开发:使用XML管理bean,注解完成属性注入
例如:在ProductService中调用ProductDao和OrderDao的方法,利用bean创立类
//扫描是为了扫描类上的注解,使用属性上的注解可以直接使用以下标签<context:annotation-config/>//在XML文件中加入bean标签创立类<bean id="productService" class="com.ProductService"></bean><bean id="productDao" class="com.ProductDao"></bean><bean id="orderDao" class="com.OrderDao"></bean>ProductService中内容:
@Resource(name="productDao")//name必需与id里的相同private ProductDao productDao;@Resource(name="orderDao")private OrderDao orderDao;public void save(){ productDao.save(); orderDao.save();}Spring+Junit测试
是spring中所提供的一个方便测试的方式,一般不用于正式开发
首先要在pom.xml中引入:
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>5.2.2.RELEASE</version></dependency>在测试类中加入注解:
@RunWith(SpringJUnit4ClassRunner.class)//指class文件下的所有xml文件@ContextConfiguration(locations= {"classpath*:/*.xml"})//或者者这样写@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration("classpath:Spring-config.xml")而后测试:
@Autowired//自动注入所需类 private AddUsersController add; @Test public void test() { add.addUser(); }1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是摆设,本站源码仅提供给会员学习使用!
7. 如遇到加密压缩包,请使用360解压,如遇到无法解压的请联系管理员
开心源码网 » 2020-03-15spring框架2