Spring基础学习

# IOC

  • spring可以预先生成对象,再将对象放到容器中,需要的时候调用

(1)步骤:

//1. 创建 Spring 的 IOC 容器
//ApplicationContext 代表IOC容器,几乎所有的应用场合都直接使用 ApplicationContext 而非底层的 BeanFactory
/*
* ApplicationContext 的主要实现类: 
* ClassPathXmlApplicationContext:从 类路径下加载配置文件
* FileSystemXmlApplicationContext: 从文件系统中加载配置文件
*/
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");


//2. 从 IOC 容器中获取 bean 的实例
HelloWorld helloWorld = (HelloWorld) ctx.getBean("helloWorld3");

//3. 使用 bean
helloWorld.hello();

(2)配置xml文件

<bean id="helloWorld" class="com.atguigu.spring.helloworld.HelloWorld">
	<!-- 为属性赋值 -->
	<property name="user" value="Jerry"></property> 
</bean>
其中,id为对象的名字,class为类名,property为对象中的属性
<!-- 通过构造器注入属性值 -->
<bean id="helloWorld3" class="com.atguigu.spring.helloworld.HelloWorld">
	<!-- 要求: 在 Bean 中必须有对应的构造器(构造方法).  -->
	<constructor-arg value="Mike"></constructor-arg>
	<!-- 若字面值中包含特殊字符, 则可以使用 DCDATA 来进行赋值. (了解) -->
	<constructor-arg>
		<value><![CDATA[<ATARZA>]]></value>
	</constructor-arg>
</bean>

<!-- 声明使用内部 bean -->
<bean id="service2" class="com.atguigu.spring.ref.Service">
	<property name="dao">
		<!-- 内部 bean, 类似于匿名内部类对象. 不能被外部的 bean 来引用, 也没有必要设置 id 属性 -->
		<bean class="com.atguigu.spring.ref.Dao">
			<property name="dataSource" value="c3p0"></property>
		</bean>
	</property>
</bean>

<!-- 装配集合属性 -->
<bean id="user" class="com.atguigu.spring.helloworld.User">
	<property name="userName" value="Jack"></property>
	<property name="cars">
		<!-- 使用 list 元素来装配集合属性 -->
		<list>
			<ref bean="car"/>
			<ref bean="car2"/>
		</list>
	</property>
</bean>

<!-- 配置Map属性值:使用map节点及map的entry子节点配置Map类型的成员变量 -->
<bean id="newPerson" class="com.atguigu.spring.helloworld.NewPerson">
	<property name="name" value="lixp"></property>
	<property name="age" value="33"></property>
	<property name="cars">
		<map>
			<entry key="AA" value-ref="car"></entry>
			<entry key="BB" value-ref="car2"></entry>
	</map>
	</property>
</bean>

<!-- 配置Properties属性值 -->
<bean id="dataSource" class="com.atguigu.spring.helloworld.DataSource">
	<property name="properties">
	<!-- 使用props和prop子节点来为Properties属性赋值 -->
		<props>
			<prop key="user">root</prop>
			<prop key="password">root@1234</prop>
			<prop key="jdbcUrl">jdbc:mysql:///test</prop>
			<prop key="driverClass">com.mysql.jdbc.Driver</prop>
		</props>
	</property>
</bean>

<!-- 可以使用property中的ref属性建立bean直接的引用关系 -->
<bean id="person" class="com.atguigu.spring.helloworld.Person">
	<property name="name" value="Tom"></property>
	<property name="age" value="24"></property>
	<property name="car" ref="car"></property>
</bean>

<!-- 自动装配: 只声明 bean, 而把 bean 之间的关系交给 IOC 容器来完成,把bean自动填充进去 -->
<!--  
	byType: 根据类型进行自动装配. 但要求 IOC 容器中只有一个类型对应的 bean, 若有多个则无法完成自动装配.
	byName: 若属性名和某一个 bean 的 id 名一致, 即可完成自动装配. 若没有 id 一致的, 则无法完成自动装配
-->
<!--Service中需要一个dao的bean,可以通过自动装配的方式,将容器里的dao对象放进去,也可以用过ref的方式  -->
<bean id="service" class="com.atguigu.spring.ref.Service" autowire="byName"></bean>
<bean id="action" class="com.atguigu.spring.ref.Action" autowire="byType"></bean>

<!-- 默认情况下 bean 是单例的! -->
<!-- 但有的时候, bean 就不能使单例的. 例如: Struts2 的 Action 就不是单例的! 可以通过 scope 属性来指定 bean 的作用域 -->
<!--  
	prototype: 原型的. 每次调用 getBean 方法都会返回一个新的 bean. 且在第一次调用 getBean 方法时才创建实例
	singleton: 单例的. 每次调用 getBean 方法都会返回同一个 bean. 且在 IOC 容器初始化时即创建 bean 的实例. 默认值 
-->
<bean id="dao2" class="com.atguigu.spring.ref.Dao" scope="prototype"></bean>

<!-- 导入外部的资源文件 -->
<context:property-placeholder location="classpath:db.properties"/>

<!-- 配置数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
	<property name="user" value="${jdbc.user}"></property>
	<property name="password" value="${jdbc.password}"></property>
	<property name="driverClass" value="${jdbc.driverClass}"></property>
	<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
	
	<property name="initialPoolSize" value="${jdbc.initPoolSize}"></property>
	<property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
</bean>

<!-- 生命周期测试 -->
<bean id="mycar" class="com.atguigu.srping.cycle.Car"
init-method="init"
destroy-method="destory">
	<property name="brand" value="Audi"></property>
</bean>

(3)注解方式

  • I、在xml文件中要申明注解,明确哪些包里的注解能生效
    <!-- 配置自动扫描的包: 需要加入 aop 对应的 jar 包 -->
	<context:component-scan 
		base-package="com.atguigu.spring.annotation"
		>
	</context:component-scan>
	<!-- 可以通过resource-pattern扫描指定的包下的内容 -->
	<!-- <context:component-scan 
		base-package="com.atguigu.spring.annotation"
		resource-pattern="generic/*.class">
	</context:component-scan> -->
	
	
	<!-- 指定排除哪些注解的内容 context:exclude-filter -->
	<!-- <context:component-scan base-package="com.atguigu.spring.annotation">
		<context:exclude-filter type="annotation" 
		expression="org.springframework.stereotype.Repository"/> 
	</context:component-scan> -->
	
	<!-- 指定只包含哪些注解的内容 需要:use-default-filters="false" +context:include-filter-->
<!-- 	<context:component-scan 
		base-package="com.atguigu.spring.annotation"
		use-default-filters="false">
		<context:include-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
	</context:component-scan> -->
  • II、spring中注解的分类:
    @Controller:控制层用于行为控制
    @Repository:持久层用于数据库交互
    @Service:服务层,用于事务逻辑
    @Commpent:普通组件
    • 使用注解的时候可以给bean的id赋值如:@Repository(“tst”),tst为id也就是对象的名字

    • @Autowired:用于自动装配,实质上就是当对象中的属性包含其他对象的时候,把该属性的对象实例化,前提是该属性的对象要先被配置

2.AOP

  • 面向切面编程,常用于打印日志(spring3)

(1)xml文件配置:

    <!-- 自动扫描的包 -->
	<context:component-scan base-package="com.atguigu.spring.aop"></context:component-scan>

	<!-- 使 AspectJ 的注解起作用 :自动为起作用的类生成代理对象-->
	<aop:aspectj-autoproxy></aop:aspectj-autoproxy>

(2)注解使用:

    1. 加入 jar 包
      com.springsource.net.sf.cglib-2.2.0.jar
      com.springsource.org.aopalliance-1.0.0.jar
      com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
      spring-aspects-4.0.0.RELEASE.jar
    1. 在 Spring 的配置文件中加入 aop 的命名空间。
    1. 基于注解的方式来使用 AOP
    • 3.1 在配置文件中配置自动扫描的包:
	<context:component-scan base-package="com.atguigu.spring.aop"></context:component-scan>
- 3.2 加入使 AspjectJ 注解起作用的配置: 
	<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
* 为匹配的类自动生成动态代理对象. 
* 
    1. 编写切面类:
    • 4.1 一个一般的 Java 类
    • 4.2 在其中添加要额外实现的功能.
    1. 配置切面
    • 5.1 切面必须是 IOC 中的 bean: 实际添加了 @Component 注解
    • 5.2 声明是一个切面: 添加 @Aspect
    • 5.3 声明通知: 即额外加入功能对应的方法.
      AspectJ 支持 5 种类型的通知注解:
      @Before: 前置通知, 在方法执行之前执行
      @After: 后置通知, 在方法执行之后执行
      @AfterRunning: 返回通知, 在方法返回结果之后执行
      @AfterThrowing: 异常通知, 在方法抛出异常之后
      @Around: 环绕通知, 围绕着方法执行
    • 5.3.1 前置通知: @Before(“execution(public int com.atguigu.spring.aop.ArithmeticCalculator.*(int, int))”)
      • @Before 表示在目标方法执行之前执行 @Before 标记的方法的方法体.
        * @Before 里面的是切入点表达式:
    1. 在通知中访问连接细节: 可以在通知方法中添加 JoinPoint 类型的参数, 从中可以访问到方法的签名和方法的参数.
    1. @After 表示后置通知: 在方法执行之后执行的代码. 无论是否出异常,都执行
    /**
    * 可以使用 @Order 注解指定切面的优先级, 值越小优先级越高
    */
    @Order(2)
    @Aspect
    @Component
	public class LoggingAspect {
	
	/**
	 * 定义一个方法, 用于声明切入点表达式. 一般地, 该方法中再不需要添入其他的代码. 
	 * 使用 @Pointcut 来声明切入点表达式. 
	 * 后面的其他通知直接使用方法名来引用当前的切入点表达式. 
	 */
	@Pointcut("execution(public int com.atguigu.spring.aop.ArithmeticCalculator.*(..))")
	public void declareJointPointExpression(){}
	
	/**
	 * 在 com.atguigu.spring.aop.ArithmeticCalculator 接口的每一个实现类的每一个方法开始之前执行一段代码
	 */
	@Before("declareJointPointExpression()")
	public void beforeMethod(JoinPoint joinPoint){
		String methodName = joinPoint.getSignature().getName();
		Object [] args = joinPoint.getArgs();
		
		System.out.println("The method " + methodName + " begins with " + Arrays.asList(args));
	}
	
	/**
	 * 在方法执行之后执行的代码. 无论该方法是否出现异常
	 */
	@After("declareJointPointExpression()")
	public void afterMethod(JoinPoint joinPoint){
		String methodName = joinPoint.getSignature().getName();
		System.out.println("The method " + methodName + " ends");
	}
	
	/**
	 * 在方法法正常结束受执行的代码
	 * 返回通知是可以访问到方法的返回值的!
	 */
	@AfterReturning(value="declareJointPointExpression()",
			returning="result")
	public void afterReturning(JoinPoint joinPoint, Object result){
		String methodName = joinPoint.getSignature().getName();
		System.out.println("The method " + methodName + " ends with " + result);
	}
	
	/**
	 * 在目标方法出现异常时会执行的代码.
	 * 可以访问到异常对象; 且可以指定在出现特定异常时在执行通知代码
	 */
	@AfterThrowing(value="declareJointPointExpression()",
			throwing="e")
	public void afterThrowing(JoinPoint joinPoint, Exception e){
		String methodName = joinPoint.getSignature().getName();
		System.out.println("The method " + methodName + " occurs excetion:" + e);
	}
	
	/**
	 * 环绕通知需要携带 ProceedingJoinPoint 类型的参数. 
	 * 环绕通知类似于动态代理的全过程: ProceedingJoinPoint 类型的参数可以决定是否执行目标方法.
	 * 且环绕通知必须有返回值, 返回值即为目标方法的返回值
	 */
	/*
	@Around("execution(public int com.atguigu.spring.aop.ArithmeticCalculator.*(..))")
	public Object aroundMethod(ProceedingJoinPoint pjd){
		
		Object result = null;
		String methodName = pjd.getSignature().getName();
		
		try {
			//前置通知
			System.out.println("The method " + methodName + " begins with " + Arrays.asList(pjd.getArgs()));
			//执行目标方法
			result = pjd.proceed();
			//返回通知
			System.out.println("The method " + methodName + " ends with " + result);
		} catch (Throwable e) {
			//异常通知
			System.out.println("The method " + methodName + " occurs exception:" + e);
			throw new RuntimeException(e);
		}
		//后置通知
		System.out.println("The method " + methodName + " ends");
		
		return result;
	}
	*/
}

3.对JDBC的支持及事务管理:spring4

(1)xml文件配置:

    <context:component-scan base-package="com.atguigu.spring"></context:component-scan>
	
	<!-- 导入资源文件 -->
	<context:property-placeholder location="classpath:db.properties"/>
	
	<!-- 配置 C3P0 数据源 -->
	<bean id="dataSource"
		class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="user" value="${jdbc.user}"></property>
		<property name="password" value="${jdbc.password}"></property>
		<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
		<property name="driverClass" value="${jdbc.driverClass}"></property>

		<property name="initialPoolSize" value="${jdbc.initPoolSize}"></property>
		<property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
	</bean>
	
	<!-- 配置 Spirng 的 JdbcTemplate -->
	<bean id="jdbcTemplate" 
		class="org.springframework.jdbc.core.JdbcTemplate">
		<property name="dataSource" ref="dataSource"></property>
	</bean>

    <!-- 配置事务管理器 -->
	<bean id="transactionManager" 
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"></property>
	</bean>

    <!-- 配置 NamedParameterJdbcTemplate, 该对象可以使用具名参数, 其没有无参数的构造器, 所以必须为其构造器指定参数 -->
	<bean id="namedParameterJdbcTemplate"
		class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">
		<constructor-arg ref="dataSource"></constructor-arg>	
	</bean>
	
	<!-- 启用事务注解 -->
	<tx:annotation-driven transaction-manager="transactionManager"/>

(2)注解方式

    @Service("bookShopService")
    public class BookShopServiceImpl implements BookShopService {

	@Autowired
	private BookShopDao bookShopDao;

	/**
	 * 添加事务注解
	 * 	1.使用 propagation 指定事务的传播行为, 即当前的事务方法被另外一个事务方法调用时
	 * 		如何使用事务, 默认取值为 REQUIRED, 即使用调用方法的事务
	 * `	REQUIRES_NEW: 事务自己的事务, 调用的事务方法的事务被挂起. 
	 * 	2.使用 isolation 指定事务的隔离级别, 最常用的取值为 READ_COMMITTED
	 * 	3.默认情况下 Spring 的声明式事务对所有的运行时异常进行回滚. 也可以通过对应的
	 * 	属性进行设置. 通常情况下去默认值即可. 
	 * 	4.使用 readOnly 指定事务是否为只读. 表示这个事务只读取数据但不更新数据, 
	 *  	这样可以帮助数据库引擎优化事务. 若真的事一个只读取数据库值的方法, 应设置 readOnly=true
	 *  	5.使用 timeout 指定强制回滚之前事务可以占用的时间. (单位为秒)
	 */
	//	@Transactional(propagation=Propagation.REQUIRES_NEW,
	//			isolation=Isolation.READ_COMMITTED,
	//			noRollbackFor={UserAccountException.class})

	//	@Transactional(propagation=Propagation.REQUIRES_NEW,
	//			isolation=Isolation.READ_COMMITTED,
	//			readOnly=false,
	//			timeout=3)
	@Transactional(
			propagation=Propagation.REQUIRES_NEW,
			isolation=Isolation.READ_COMMITTED,
			readOnly=false,
			timeout=3
			)
	@Override
	public void purchase(String username, String isbn) {

		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {}

		//1. 获取书的单价
		int price = bookShopDao.findBookPriceByIsbn(isbn);

		//2. 更新数的库存
		bookShopDao.updateBookStock(isbn);

		//3. 更新用户余额
		bookShopDao.updateUserAccount(username, price);
	}

}

4.拓展:如何在 Web 应用中使用Spring

方法一:

(1)导入jar包:

额外需要导入jar包

  • spring-web-4.0.0.RELEASE.jar
  • spring-webmvc-4.0.0.RELEASE.jar
(2)创建spring的配置文件
	<bean id="person"
		class="com.atguigu.spring.struts2.beans.Person">
		<property name="username" value="atguigu"></property>
	</bean>
(3)在 ServletContextListener#contextInitialized(ServletContextEvent sce) 方法中创建 IOC 容器.
  1. 创建一个Create Listener文件
  2. 选择next,勾选Lifecycle
  3. 在web.xml文件中进行配置
 <!-- 配置Spring配置文件的名称和位置 -->
  <context-param>
    <param-name>configLocation</param-name>
    <param-value>applicationContext.xml</param-value>
  </context-param>
  <!-- 启动IOC容器的SerletContextListener -->
  <listener>
    <listener-class>com.atguigu.spring.struts2.listeners.SpringServletContextListener</listener-class>
  </listener>
  1. 重写contextInitialized方法
    public void contextInitialized(ServletContextEvent arg0) {
    	//1. 获取 Spring 配置文件的名称. 
    	ServletContext servletContext = arg0.getServletContext();
    	String config = servletContext.getInitParameter("configLocation");
    	
    	//2. 创建 IOC 容器
    	ApplicationContext ctx = new ClassPathXmlApplicationContext(config);
    	
    	//3. 把 IOC 容器放在 ServletContext 的一个属性中. 
    	servletContext.setAttribute("ApplicationContext", ctx);
    }
  1. 创建TestServlet(测试)
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//1. 从 application 域对象中得到 IOC 容器的引用
		ServletContext servletContext = getServletContext();
		ApplicationContext ctx = (ApplicationContext) servletContext.getAttribute("ApplicationContext");
		
		//2. 从 IOC 容器中得到需要的 bean
		Person person = ctx.getBean(Person.class);
		person.hello();
	}

方法二:

(1)导入jar包
(2)创建spring的配置文件
(3)配置在 web.xml 文件[ALT+/ 选择倒数第三个]
<!-- 配置 Spring 配置文件的名称和位置 -->
<context-param>
	<param-name>contextConfigLocation</param-name>
	<param-value>classpath:applicationContext.xml</param-value>
</context-param>

<!-- 启动 IOC 容器的 ServletContextListener -->
<listener>
	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
(4)测试
        //1. 从 appication 域对象中得到 IOC 容器的实例
		ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(application);
		
		//2. 从 IOC 容器中得到 bean
		Person person = ctx.getBean(Person.class);
		
		//3. 使用 bean
		person.hello();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值