java-Spring

Spring

  • Spring IOC/DI

Spring是一个基于IOC和AOP的结构J2EE系统的框架
IOC 反转控制 是Spring的基础,Inversion Of Control
简单说就是创建对象由以前的程序员自己new 构造方法来调用,变成了交由Spring创建对象
DI 依赖注入 Dependency Inject. 简单地说就是拿到的对象的属性,已经被注入好相关值了,直接使用即可。

1.新建项目

在工作目录下新建spring项目 (java project类型)
在这里插入图片描述
2.下载包
下载所需jar包,并解压lib 目录下
在这里插入图片描述
3.导入包

把jar包导入到项目中,导包办法:右键 project->properties->java build path->libaries->add external jars
在这里插入图片描述
4.pojo

准备pojo Category,用来演示IOC和DI
在这里插入图片描述

package com.how2java.pojo;

public class Category {
	private int id;
	private String name;
	public void setId(int id){
		this.id=id;
	}
	public int getId(){
		return id;
	}
	public void setName(String name){
		this.name=name;
	}
	public String getName(){
		return name;
	}
}

5.applicationContext.xml

在src目录下新建applicationContext.xml文件
applicationContext.xml是Spring的核心配置文件,通过关键字c即可获取Category对象,该对象获取的时候,即被注入了字符串"category 1“到name属性中

<?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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
   http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/aop
   http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
   http://www.springframework.org/schema/tx
   http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
   http://www.springframework.org/schema/context     
   http://www.springframework.org/schema/context/spring-context-3.0.xsd">
  
    <bean name="c" class="com.how2java.pojo.Category">
        <property name="name" value="category 1" />
    </bean>
</beans>

6.TestSpring

测试代码,演示通过spring获取Category对象,以及该对象被注入的name属性。
如图所示,可以打印出通过Spring拿到的Category对象的name属性

package com.how2java.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.how2java.pojo.Category;

public class TestSpring {
	public static void main(String[] args){
		//创建Spring的IOC容器对象
		ApplicationContext context=new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"});
		//从IOC容器获取Bean实例:在IOC容器里利用id定位到对应的bean
		Category c=(Category)context.getBean("c");
		System.out.println(c.getName());
	}
}

在这里插入图片描述
7.原理图

以获取对象的方式来进行比较

传统的方式:
通过new 关键字主动创建一个对象
IOC方式
对象的生命周期由Spring来管理,直接从Spring那里去获取一个对象。
IOC是反转控制 (Inversion Of Control)的缩写,就像控制权从本来在自己手里,交给了Spring。

打个比喻:
传统方式:相当于你自己去菜市场new 了一只鸡,不过是生鸡,要自己拔毛,去内脏,再上花椒,酱油,烤制,经过各种工序之后,才可以食用。
用 IOC:相当于去馆子(Spring)点了一只鸡,交到你手上的时候,已经五味俱全,你就只管吃就行了。

在这里插入图片描述
8.练习-使用IOC的方式,获取一个Product对象

在这里插入图片描述

package com.how2java.pojo;
public class Product {
	private int id;
    private String name;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}
package com.how2java.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.how2java.pojo.Category;
import com.how2java.pojo.Product;

public class TestSpring {
	public static void main(String[] args){
		/*ApplicationContext context=new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"});
		Category c=(Category)context.getBean("c");
		System.out.println(c.getName());*/
		ApplicationContext context=new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"});
		Product p=(Product)context.getBean("p");
		System.out.println(p.getName());
	}
}

在这里插入图片描述

  • 注入对象

在上例中,对Category的name属性注入了"category 1"字符串
在本例中 ,对Product对象,注入一个Category对象

1.Product.java

Product类中有对Category对象的setter getter

package com.how2java.pojo;
public class Product {
	private int id;
    private String name;
    private Category category;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Category getCategory(){
    	return category;
    }
    public void setCategory(Category category){
    	this.category=category;
    }
}

2.applicationContext.xml

在创建Product的时候注入一个Category对象
注意,这里要使用ref来注入另一个对象

<?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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
   http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/aop
   http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
   http://www.springframework.org/schema/tx
   http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
   http://www.springframework.org/schema/context     
   http://www.springframework.org/schema/context/spring-context-3.0.xsd">
  
    <bean name="c" class="com.how2java.pojo.Category">
    		<property name="name" value="category 1" />
    </bean>
    <bean name="p" class="com.how2java.pojo.Product">
    		<property name="name" value="product 1" />
    		<property name="category" ref="c" />
    </bean>
</beans>

3.TestSpring

通过Spring拿到的Product对象已经被注入了Category对象了

package com.how2java.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.how2java.pojo.Category;
import com.how2java.pojo.Product;

public class TestSpring {
	public static void main(String[] args){
		ApplicationContext context=new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"});
		Product p=(Product)context.getBean("p");
		System.out.println(p.getName());
		System.out.println(p.getCategory().getName());
	}
}

在这里插入图片描述

  • 注解方式 IOC/DI

在本知识点中,将演示如何使用注解的方式完成注入对象中的效果。

1.修改applicationContext.xml

  1. 在17行添加
    context:annotation-config/
    表示告诉Spring要用注解的方式进行配置
  1. 注入对象的23行注释掉,这个行为在后面将使用注解来完成
<?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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
   http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/aop
   http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
   http://www.springframework.org/schema/tx
   http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
   http://www.springframework.org/schema/context     
   http://www.springframework.org/schema/context/spring-context-3.0.xsd">
  	
  	<context:annotation-config />
    <bean name="c" class="com.how2java.pojo.Category">
    		<property name="name" value="category 1" />
    </bean>
    <bean name="p" class="com.how2java.pojo.Product">
    		<property name="name" value="product 1" />
    		<!-- <property name="category" ref="c" /> -->
    </bean>
</beans>

2.@Autowired

在Product.java的category属性前加上@Autowired注解

@Autowired
private Category category;

package com.how2java.pojo;

import org.springframework.beans.factory.annotation.Autowired;

public class Product {
	private int id;
    private String name;
    @Autowired
    private Category category;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Category getCategory(){
    	return category;
    }
    public void setCategory(Category category){
    	this.category=category;
    }
}

3.运行测试

package com.how2java.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.how2java.pojo.Category;
import com.how2java.pojo.Product;

public class TestSpring {
	public static void main(String[] args){
		ApplicationContext context=new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"});
		Product p=(Product)context.getBean("p");
		System.out.println(p.getName());
		System.out.println(p.getCategory().getName());
	}
}

在这里插入图片描述
4.@Autowired的位置

除了前面的 在属性前加上@Autowired 这种方式外,也可以在setCategory方法前加上@Autowired,这样来达到相同的效果

@Autowired
public void setCategory(Category category)

package com.how2java.pojo;

import org.springframework.beans.factory.annotation.Autowired;

public class Product {
	private int id;
    private String name;
    private Category category;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Category getCategory(){
    	return category;
    }
    @Autowired
    public void setCategory(Category category){
    	this.category=category;
    }
}

运行测试
在这里插入图片描述
5.@Resource

除了@Autowired之外,@Resource也是常用的手段

@Resource(name=“c”)
private Category category;

package com.how2java.pojo;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;

public class Product {
	private int id;
    private String name;
    @Resource(name="c")
    private Category category;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Category getCategory(){
    	return category;
    }
    public void setCategory(Category category){
    	this.category=category;
    }
}

在这里插入图片描述
6.对Bean的注解

上述例子是对注入对象行为的注解,那么bean对象本身,比如Category,Product可不可以移出applicationContext.xml配置文件,也通过注解进行呢?
接下来就讲解如何对Bean进行注解配置

7.applicationContext.xml

修改applicationContext.xml,什么都去掉,只新增:

<context:component-scan base-package=“com.how2java.pojo”/>

其作用是告诉Spring,bean都放在com.how2java.pojo这个包下

<?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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
   http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/aop
   http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
   http://www.springframework.org/schema/tx
   http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
   http://www.springframework.org/schema/context     
   http://www.springframework.org/schema/context/spring-context-3.0.xsd">
  	
	<!-- <context:annotation-config />
    <bean name="c" class="com.how2java.pojo.Category">
    		<property name="name" value="category 1" />
    </bean>
    <bean name="p" class="com.how2java.pojo.Product">
    		<property name="name" value="product 1" />
    		<property name="category" ref="c" />
    </bean> -->
    <context:component-scan base-package="com.how2java.pojo"/>
    
</beans>

8.@Component

为Product类加上@Component注解,即表明此类是bean

@Component(“p”)
public class Product {

为Category 类加上@Component注解,即表明此类是bean

@Component(“c”)
public class Category {

另外,因为配置从applicationContext.xml中移出来了,所以属性初始化放在属性声明上进行了。

private String name=“product 1”;
private String name=“category 1”;

package com.how2java.pojo;

import org.springframework.stereotype.Component;

@Component("c")
public class Category {
	private int id;
	private String name="category 1";
	public void setId(int id){
		this.id=id;
	}
	public int getId(){
		return id;
	}
	public void setName(String name){
		this.name=name;
	}
	public String getName(){
		return name;
	}
}
package com.how2java.pojo;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component("p")
public class Product {
	private int id;
    private String name="product 1";
    @Autowired
    private Category category;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Category getCategory(){
    	return category;
    }
    public void setCategory(Category category){
    	this.category=category;
    }
}

运行测试:运行TestSpring,可以发现运行结果是一样的

package com.how2java.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.how2java.pojo.Category;
import com.how2java.pojo.Product;

public class TestSpring {
	public static void main(String[] args){
		ApplicationContext context=new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"});
		Product p=(Product)context.getBean("p");
		System.out.println(p.getName());
		System.out.println(p.getCategory().getName());
	}
}

在这里插入图片描述

  • AOP

spect Oriented Program 面向切面编程
首先,在面向切面编程的思想里面,把功能分为 核心业务功能 ,和 周边功能
所谓的核心业务,比如登陆,增加数据,删除数据都叫核心业务
所谓的周边功能,比如性能统计,日志,事务管理等等

周边功能在Spring的面向切面编程AOP思想里,即被定义为 切面
在面向切面编程AOP的思想里面,核心业务功能和切面功能分别 独立进行开发
然后把切面功能和核心业务功能 “编织” 在一起,这就叫AOP

1.导入额外的jar包

为了支持AOP,需要用到一些额外的JAR包。

2.思路图

1.功能分两大类,辅助功能和核心业务功能
2.辅助功能和核心业务功能彼此独立进行开发
3.比如登陆功能,即便是没有性能统计和日志输出,也可以正常运行
4.如果有需要,就把"日志输出" 功能和 “登陆” 功能 编织在一起,这样登陆的时候,就可以看到日志输出了
5.辅助功能,又叫做切面,这种能够选择性的,低耦合的把切面和核心业务功能结合在一起的编程思想,就叫做切面编程

在这里插入图片描述
3.准备业务类 ProductService和配置文件applicationContext.xml

package com.how2java.service;
public class ProductService {
	public void doSomeService(){
		System.out.println("doSomeService");
	}
}
<?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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
   http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/aop
   http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
   http://www.springframework.org/schema/tx
   http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
   http://www.springframework.org/schema/context     
   http://www.springframework.org/schema/context/spring-context-3.0.xsd">
  	
  	<bean name="s" class="com.how2java.service.ProductService">
  	
  	</bean>
</beans>

5.TestSpring

在引入切面之前,调用该业务类

package com.how2java.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.how2java.service.ProductService;

public class TestSpring {
	public static void main(String[] args){
		ApplicationContext context=new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"});
		ProductService s=(ProductService)context.getBean("s");
		s.doSomeService();
	}
}

在这里插入图片描述
6.准备日志切面 LoggerAspect

该日志切面的功能是 在调用核心功能之前和之后分别打印日志,切面就是原理图中讲的那些辅助功能。

Object object = joinPoint.proceed();

就是将来与某个核心功能编织之后,用于执行核心功能的代码

package com.how2java.aspect;

import org.aspectj.lang.ProceedingJoinPoint;

public class LoggerAspect {
	public Object log(ProceedingJoinPoint joinPoint) throws Throwable{
		System.out.println("start log:"+joinPoint.getSignature().getName());
		Object object=joinPoint.proceed();
		System.out.println("end log:"+joinPoint.getSignature().getName());
		return object;
	}
}

7.applicationContext.xml

< bean name=“s” class=“com.how2java.service.ProductService”>
< /bean>

声明业务对象

< bean id=“loggerAspect” class=“com.how2java.aspect.LoggerAspect”/>

声明日志切面
结合思路图
指定右边的核心业务功能

<aop:pointcut id="loggerCutpoint" 
		expression=
		"execution(* com.how2java.service.ProductService.*(..)) "/>

指定左边的辅助功能

<aop:aspect id="logAspect" ref="loggerAspect">
	<aop:around pointcut-ref="loggerCutpoint" method="log"/>
</aop:aspect>

然后通过aop:config把业务对象与辅助功能编织在一起。
在这里插入图片描述

execution(* com.how2java.service.ProductService.(…))
这表示对满足如下条件的方法调用,进行切面操作:
==
== 返回任意类型
com.how2java.service.ProductService.* 包名以 com.how2java.service.ProductService 开头的类的任意方法
(…) 参数是任意数量和类型

<?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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
   http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/aop
   http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
   http://www.springframework.org/schema/tx
   http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
   http://www.springframework.org/schema/context     
   http://www.springframework.org/schema/context/spring-context-3.0.xsd">
  	
  	<bean name="s" class="com.how2java.service.ProductService">
  	</bean>
  	
  	<bean id="loggerAspect" class="com.how2java.aspect.LoggerAspect" />
  	
  	<aop:config>
  		<aop:pointcut id="loggerCutpoint"
  			 expression=
  			 "execution(* com.how2java.service.ProductService.*(..))" />
  		<aop:aspect id="logAspect" ref="loggerAspect">
  			<aop:around pointcut-ref="loggerCutpoint" method="log" />
  		</aop:aspect>
  	</aop:config>
  	
</beans>

8.TestSpring

TestSpring 代码没有发生任何变化,通过配置的方式,把切面和核心业务类编制在了一起。
运行测试,可以发现在编织之后,业务方法运行之前和之后分别会打印日志

package com.how2java.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.how2java.service.ProductService;

public class TestSpring {
	public static void main(String[] args){
		ApplicationContext context=new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"});
		ProductService s=(ProductService)context.getBean("s");
		s.doSomeService();
	}
}

在这里插入图片描述
9.练习-性能统计切面

参考上述的日志切面,做一个性能统计切面,并编织在业务方法上面。
注;在业务方法方法中,做一些JDBC访问,以增加耗时

  • 注解方式 AOP

1.注解配置业务类

使用@Component(“s”) 注解ProductService 类

package com.how2java.service;
import org.springframework.stereotype.Component;
@Component("s")
public class ProductService {
	public void doSomeService(){
		System.out.println("doSomeService");
	}
}

2.注解配置切面

@Aspect 注解表示这是一个切面
@Component 表示这是一个bean,由Spring进行管理
@Around(value = “execution(* com.how2java.service.ProductService.*(…))”) 表示对com.how2java.service.ProductService 这个类中的所有方法进行切面操作

package com.how2java.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class LoggerAspect {
	@Around(value="execution(* com.how2java.service.ProductService.*(..))")
	public Object log(ProceedingJoinPoint joinPoint) throws Throwable{
		System.out.println("start log:"+joinPoint.getSignature().getName());
		Object object=joinPoint.proceed();
		System.out.println("end log:"+joinPoint.getSignature().getName());
		return object;
	}
}

3.applicationContext.xml

去掉原有信息,添加如下3行

<context:component-scan base-package="com.how2java.aspect"/>
<context:component-scan base-package="com.how2java.service"/>

扫描包com.how2java.aspect和com.how2java.service,定位切面类和业务类

<aop:aspectj-autoproxy/>  

找到被注解了的切面类,进行切面配置

<?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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
   http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/aop
   http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
   http://www.springframework.org/schema/tx
   http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
   http://www.springframework.org/schema/context     
   http://www.springframework.org/schema/context/spring-context-3.0.xsd">
  	
  	<context:component-scan base-package="com.how2java.aspect"/>
  	<context:component-scan base-package="com.how2java.service"/>
  	<aop:aspectj-autoproxy />
</beans>

4.运行测试

package com.how2java.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.how2java.service.ProductService;

public class TestSpring {
	public static void main(String[] args){
		ApplicationContext context=new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"});
		ProductService s=(ProductService)context.getBean("s");
		s.doSomeService();
	}
}

在这里插入图片描述

  • 注解方式测试

1.jar

注解方式用到了junit,导入junit-4.12.jar和hamcrest-all-1.3.jar

2.Category

package com.how2java.pojo;

import org.springframework.stereotype.Component;

public class Category {
	private int id;
	private String name;
	public void setId(int id){
		this.id=id;
	}
	public int getId(){
		return id;
	}
	public void setName(String name){
		this.name=name;
	}
	public String getName(){
		return name;
	}
}

3.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"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
   http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/aop
   http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
   http://www.springframework.org/schema/tx
   http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
   http://www.springframework.org/schema/context     
   http://www.springframework.org/schema/context/spring-context-3.0.xsd">
  	
  	<!-- <context:component-scan base-package="com.how2java.aspect"/>
  	<context:component-scan base-package="com.how2java.service"/>
  	<aop:aspectj-autoproxy /> -->
  	 <bean name="c" class="com.how2java.pojo.Category">
        <property name="name" value="category 1" />
    </bean>
</beans>

4.TestSpring

修改TestSpring, 并运行

1.@RunWith(SpringJUnit4ClassRunner.class)
表示这是一个Spring的测试类
2.@ContextConfiguration(“classpath:applicationContext.xml”)
定位Spring的配置文件
3.@Autowired
给这个测试类装配Category对象
4.@Test
测试逻辑,打印c对象的名称

package com.how2java.test;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.how2java.pojo.Category;


@RunWith(SpringJUnit4ClassRunner.class) //表示这是一个Spring的测试类
@ContextConfiguration("classpath:applicationContext.xml") //定位Spring的配置文件
public class TestSpring {
		@Autowired
		Category c;
		
		@Test
		public void test(){
			System.out.println(c.getName());
		}
}

在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
pulsar-java-spring-boot-starter是一个用于在Spring Boot应用程序中集成Apache Pulsar消息队列的开源库。Apache Pulsar是一个可扩展的、低延迟的分布式消息传递平台,它具有高吞吐量和高可靠性的特点。 pulsar-java-spring-boot-starter允许开发人员在Spring Boot应用程序中轻松地发送和接收Pulsar消息。它提供了一组容易使用的注解和工具类,简化了与Pulsar集群的交互。 使用pulsar-java-spring-boot-starter,开发人员可以通过添加依赖和配置一些属性来快速集成Pulsar到他们的Spring Boot应用程序中。一旦集成完成,开发人员可以使用注解来定义消息的生产者和消费者。通过生产者注解,开发人员可以将消息发送到Pulsar集群,并指定消息的主题和内容。通过消费者注解,开发人员可以订阅Pulsar主题,并定义接收和处理消息的方法。 除了基本的生产者和消费者功能,pulsar-java-spring-boot-starter还提供了一些其他特性。例如,它支持失败重试机制,当消息发送或接收出现问题时,可以自动重试。它还支持消息过滤器,可以按条件过滤接收的消息。而且,它还提供了一些监控和管理功能,可以方便地监控消息的生产和消费情况。 总之,pulsar-java-spring-boot-starter为Spring Boot开发人员提供了一种方便、快捷地集成Apache Pulsar消息队列的方法。它简化了与Pulsar集群的交互,提供了易于使用的注解和工具类,让开发人员可以更专注于业务逻辑的实现。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值