Spring---annotation注解初学

Spring如何使用注解

1、新建一个java工程,添加spring3.1支持

2、配置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:p="http://www.springframework.org/schema/p"
	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.1.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
	
	<!-- 1、引入命名空间context 
		 xmlns:context="http://www.springframework.org/schema/context"
		 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
	 -->
	<!-- 2、引入spring的注解配置 -->
	<context:annotation-config></context:annotation-config>
	<!-- 3、指定spring注解扫描的包 -->
	<context:component-scan base-package="com.etc"></context:component-scan>

	<!-- 默认配置,现在不在配置文件中进行bean的配置,而是在代码中加入注解来完成访问 -->
	<!-- <bean name="helloService" class="com.etc.service.HelloService"></bean> -->

</beans>

3、新建com.etc.service包,在包下新建HelloService.java类:

package com.etc.service;

import org.springframework.stereotype.Component;

//自动首字母小写,后面不变,此处配置的是helloService
@Component
public class HelloService {
	public void sayHello(){
		System.out.println("hello");
	}
}

4、新建test源文件夹,在文件夹下新建com.etc.test包,在包下新建HelloServiceTest.java类:

package com.etc.test;

import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.etc.service.HelloService;
import com.etc.service.MyService;

public class HelloServiceTest {
	@Test
	public void test1(){
		//1、加载spring配置
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
		//2、通过配置文件获取所需要的类,不需要新建类的对象
		HelloService helloService = (HelloService) context.getBean("helloService");
		//3、调用这个类的方法
		helloService.sayHello();
	
	}
}

由此可见,我们只需要在HelloService.java类上面添加注解@Component注解,就相当于在applicationContext.xml中配置了如下信息:

<bean name="helloService" class="com.etc.service.HelloService"></bean>
使得我们可以使用helloService来访问HelloService.java类


如果我们想要新建一个bean,代码变得很容易:在com.etc.service下新建MyService.java类:

package com.etc.service;

import org.springframework.stereotype.Component;

//想要新建一个bean,只需要在代码中添加注解就可以了,不需要在配置文件中进行任何的配置
@Component
public class MyService {
	public void say(){
		System.out.println("this is myService");
	}
}

新建一个MyServiceTest.java测绘类,就可以直接访问MyService.java类了

package com.etc.test;

import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.etc.service.HelloService;
import com.etc.service.MyService;

public class MyServiceTest {
	@Test
	public void test1(){
		//1、加载spring配置
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
		//2、通过配置文件获取所需要的类,不需要新建类的对象
		MyService myService = (MyService)context.getBean("myService");
		//3、调用这个类的方法
		myService.say();
	}
}
不需要在配置文件中添加任何的配置,只需要在对应的类中增加一个注解,就可以实现。

注释配置相对于 XML 配置具有很多的优势:

  • 它可以充分利用 Java 的反射机制获取类结构信息,这些信息可以有效减少配置的工作。如使用 JPA 注释配置 ORM 映射时,我们就不需要指定 PO 的属性名、类型等信息,因为这些信息都可以通过 Java 反射机制获取。
  • 注解和 Java 代码位于一个文件中,而 XML 配置采用独立的配置文件,大多数配置信息在程序开发完成后都不会调整,如果配置信息和 Java 代码放在一起,有助于增强程序的内聚性。而采用独立的 XML 配置文件,程序员在编写一个功能时,往往需要在程序文件和配置文件中不停切换,这种思维上的不连贯会降低开发效率。
弊端:

使用注解的时候,配置分布在各个类上,不便于查找,而使用配置文件,所有的配置都在其中,集中在一起,方便查找

@Component 

是所有受Spring 管理组件的通用形式,@Component注解可以放在类的头上,@Component不推荐使用。

@Repository  用于dao层(持久层)

@Service   用于service层(服务层)

@Controller 用于action层(控制层)

他们的作用其实都是一样的,只是为了代码的清晰可见,所以针对不同的层使用不同的注解。

@Autowired

package com.etc.dao;
import org.springframework.stereotype.Repository;

@Repository("dao")
public class MyDAO implements IMyDAO {

	@Override
	public void mydao() {
		System.out.println("mydao method");
	}

}

package com.etc.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.etc.dao.IMyDAO;

@Service
public class MyService {
	@Autowired //根据名字 (此处的dao和MyDAO中的dao相同)自动注入
	private IMyDAO dao;
	public void say(){
		System.out.println("this is myService");
		dao.mydao();
	}
	public IMyDAO getDao() {
		return dao;
	}
	public void setDao(IMyDAO dao) {
		this.dao = dao;
	}
}
package com.etc.test;

import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.etc.service.HelloService;
import com.etc.service.MyService;

public class MyServiceTest {
	@Test
	public void test1(){
		//1、加载spring配置
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
		//2、通过配置文件获取所需要的类,不需要新建类的对象
		MyService myService = (MyService)context.getBean("myService");
		//3、调用这个类的方法
		myService.say();
	}
}
运行结果:

this is myService
mydao method


表示MyService中注入MyDAO成功了,不需要再applicationContext.xml中进行任何的配置,都在java代码中完成。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小梦想的博客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值