spring的基本知识
来源:互联网 发布:知乎女神扒皮 编辑:程序博客网 时间:2024/05/24 03:35
一、新建Java Project项目
二、创建lib文件夹,导入包
三、在src下创建java类与applicationContext.xml
Student类需要有无参构造函数
package com.xu;public class Student {private String name;private String sex;public String getName() {return name;}public void setName(String name) {this.name = name;}public String getSex() {return sex;}public void setSex(String sex) {this.sex = sex;}@Overridepublic String toString() {return "student [name=" + name + ", sex=" + sex + "]";}public Student(String name, String sex) {super();this.name = name;this.sex = sex;}public Student(){}}
applicationContext.xml文件内容
<?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="student" class="com.xu.Student"> <property name="name" value="me"></property> <property name="sex" value="girl"></property> </bean> <!-- 引用外部bean 时使用ref属性 <property name="book" ref="book"></property> //外部bean <bean id="book" class="com.xu.Book"> <property name="name" value="math"></property> </bean> --></beans>
在run中运行
package com.xu;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class run {public static void main(String[] args){ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");Student stu= (Student)ac.getBean("student");System.out.println(stu.toString());}}
四、用Spring的命名空间p:去实现bean中<property>标签的功能
注意,在使用命名空间p之前,要在beans标签中添加 xmlns:p="http://www.springframework.org/schema/p"
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="student" class="com.xu.Student" p:name="me" p:sex="girl" p:book-ref="book"> </bean> <bean id="book" class="com.xu.Book" p:name="math"> </bean> </beans>
五、引用外部属性文件
创建db.properties文件
内容为:
user="user"sex="girl"
applicationContext.xml文件中<bean>的配置
引用外部属性文件之前,要在beans标签中添加xmlns:context="http://www.springframework.org/schema/context",及相应约束
详见示例代码:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"><!-- 导入属性文件 --><context:property-placeholder location="classpath:db.properties"/> <bean id="book" class="com.xu.Book" p:name="math"> </bean> <!-- 使用类似EL的语法获取属性文件中的内容 --> <bean id="data" class="com.xu.Student"> <property name="name" value="${user}"></property> <property name="sex" value="${sex}"></property> <property name="book" ref="book"></property> </bean> </beans>
六、通过FactoryBean配置bean
一般情况下,Spring通过反射机制利用bean的class属性指定实现类来实例化bean 。在某些情况下,实例化bean过程比较复杂,如果按照传统的方式,则需要在<bean>中提供大量的配置信息,配置方式的灵活性是受限的,这时采用编码的方式可能会得到一个简单的方案。Spring为此提供了一个org.Springframework.bean.factory.FactoryBean的工厂类接口,用户可以通过实现该接口定制实例化bean的逻辑。
package org.Springframework.beans.factory; public interface FactoryBean<T> { T getObject() throws Exception; Class<?> getObjectType(); boolean isSingleton(); }
在该接口中还定义了以下3个方法。
T getObject():返回由FactoryBean创建的bean实例,如果isSingleton()返回true,则该实例会放到Spring容器中单实例缓存池中。
boolean isSingleton():返回由FactoryBean创建的bean实例的作用域是singleton还是prototype。
Class<T> getObjectType():返回FactoryBean创建的bean类型。
package com.xu;public class Car {private String name;private int price;public String getName() {return name;}public void setName(String name) {this.name = name;}public int getPrice() {return price;}public void setPrice(int price) {this.price = price;}@Overridepublic String toString() {return "Car [name=" + name + ", price=" + price + "]";}}
如果使用传统方式配置下面Car的<bean>时,Car的每个属性分别对应一个<property>元素标签。
如果用FactoryBean的方式实现就会灵活一些。
public class CarFactoryBean implements FactoryBean<Car> {@Overridepublic boolean isSingleton() {///返回由FactoryBean创建的bean实例的作用域是singleton还是prototype。return false;}@Overridepublic Car getObject() throws Exception {///返回由FactoryBean创建的bean实例,如果isSingleton()返回true,则该实例会放到Spring容器中单实例缓存池中。Car car=new Car();car.setName("new car");car.setPrice(3000000);return car;}@Overridepublic Class<Car> getObjectType() {//返回FactoryBean创建的bean类型。return Car.class;}}
applicationContext中的bean配置
<bean id="beanFactory" class="com.xu.CarFactoryBean"> </bean>也可以在此bean中传递参数,在GetObject()中处理、赋值。
当调用getBean("car") 时,Spring通过反射机制发现CarFactoryBean实现了FactoryBean的接口,这时Spring容器就调用接口方法CarFactoryBean#getObject()方法返回。如果希望获取CarFactoryBean的实例,则需要在使用getBean(beanName) 方法时在beanName前显示的加上 "&" 前缀,例如getBean("&car")。
七、注解配置bean(自动扫描包)
给类添加注解
@Component (基本注解,表示受spring管理的组件)
@Service (服务层组件)
@Repository (持久层组件)
@Controller (表现层组件)
如:
@Servicepublic class Pen {public Pen(){System.out.println("Pen......@Service");}}
在ApplicationContext中配置自动扫描包
提示:自动扫描包 需要引用spring-aop.jar 包,否则会报错
<!-- 自动扫描包 --> <context:component-scan base-package="com.xu.scan"></context:component-scan>扫描到的类会被放入IOC容器中
- spring的基本知识
- Java Spring的基本知识
- 学习Spring的基本知识
- 关于spring的基本知识介绍
- Spring的基本知识(入门)
- Spring基本知识
- Spring基本知识
- Spring基本知识1
- Spring框架基本知识
- Spring Data JPA基本知识
- Spring面试基本知识
- Spring面试基本知识2
- Spring 事务学习笔记(1):事务的基本知识
- spring jpa data使用的基本知识和注意事项
- Indie音乐的基本知识
- UNIX的一些基本知识
- 接口技术的基本知识
- python的基本知识
- js split方法针对单个"\"反斜杠
- java.lang.reflect.UndeclaredThrowableException
- 线段树 --- 适用于修改性的求区间问题 【单点修改】
- Hibernate学习笔记 -- day01 Hibernate介绍及入门案例环境搭建
- 常用的知识点纪录
- spring的基本知识
- python--文件操作(I/O)
- TFTP与FTP的比较
- fzu 2233 贪心并查集
- Python——pyquery解析html
- Spring 中注解
- C++的回调机制
- Intellij idea/Servlet学习之-在Servlet中实现页面转发
- AMD&CMD&CommonJS&RequireJS