java注解配置spring

新建applicationContext.xml
在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: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">

	<!-- add entry to enable component scanning -->
	
	<context:component-scan base-package="com.luv2code.springdemo" />
</beans>

添加注解

package com.luv2code.springdemo;

import org.springframework.stereotype.Component;

@Component("thisSillyCoach")
public class TennisCoach implements Coach {

	@Override
	public String getDailyWorkout() {
		// TODO Auto-generated method stub
		return "practice your backhand volley";
	}

}

package com.luv2code.springdemo;

public interface Coach {
	public String getDailyWorkout();
}

从spring容器中获取bean

package com.luv2code.springdemo;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class AnnotationDemo {

	public static void main(String[] args) {
		//read spring config file
		ClassPathXmlApplicationContext context = 
				new ClassPathXmlApplicationContext("applicationContext.xml");
		
		//get the bean from spring container
		Coach theCoach = context.getBean("thisSillyCoach",Coach.class);
		//call a method on the bean
		System.out.println(theCoach.getDailyWorkout());
		
		//close the context
		context.close();
	}

}

输出结果:

practice your backhand volley
2.可以用默认的注解名获取bean
@Component   //改为@Component
public class TennisCoach implements Coach {

	@Override
	public String getDailyWorkout() {
		// TODO Auto-generated method stub
		return "practice your backhand volley";
	}

}

在实现类里面的beanid改为获取类的首字母小写的类名tennisCoach

		//get the bean from spring container
		Coach theCoach = context.getBean("tennisCoach",Coach.class);
一、用注解注入依赖
public interface FortuneService {
	
	public String getFortune();
	
}

public interface Coach {
	public String getDailyFortune();
}

@Component
public class HappyFortuneService implements FortuneService {
	@Override
	public String getFortune() {
		
		return "Today is your lucky day!";
	}
}

一个构造函数 @Autowired 说明当创建 bean 时,即使在 XML 文件中没有使用 元素配置 bean ,构造函数也会被自动连接

@Component
public class TennisCoach implements Coach {

	private FortuneService fortuneService;
	@Autowired
	public TennisCoach(FortuneService theFortuneService) {
		fortuneService = theFortuneService;
	}
	@Override
	public String getDailyFortune() {
		return fortuneService.getFortune();
	}
}

public class AnnotationDemo {

	public static void main(String[] args) {
		//read spring config file
		ClassPathXmlApplicationContext context = 
				new ClassPathXmlApplicationContext("applicationContext.xml");
		//get the bean from spring container
		Coach theCoach = context.getBean("tennisCoach",Coach.class);
		//call method to get the daily fortune
		System.out.println(theCoach.getDailyFortune());
		//close the context
		context.close();
	}

}
Today is your lucky day!
二、使用setter建立依赖

在tennisCoach类中更改构造方法:

	//define a default constructor
	public TennisCoach() {
		System.out.println(">> TennisCoach: inside default constructor");
	}
	@Autowired     //set方法换成其他名称同样适用
	public void setFortuneService(FortuneService theFortuneService) {
		System.out.println(">> TennisCoach: inside setFortuneService() method");
		fortuneService = theFortuneService;
	}

重新运行AnnotationDemo:

>> TennisCoach: inside default constructor
>> TennisCoach: inside setFortuneService() method
Today is your lucky day!
三、使用作用域配置
public class TennisCoach implements Coach {
	@Autowired
	private FortuneService fortuneService;
	...
	}
如果FortuneService接口被多个类继承,那么TennisCoach类在调用的时候使用的是哪一个呢?

例如创建RandomService ,RESTFortuneService 类:

@Component
public class RandomService implements FortuneService {

	@Override
	public String getFortune() {
		// TODO Auto-generated method stub
		return "RandomService";
	}

}

@Component
public class DatabaseFortune implements FortuneService {

	@Override
	public String getFortune() {
		// TODO Auto-generated method stub
		return "DatabaseService";
	}

}
@Component
public class RESTFortuneService implements FortuneService {

	@Override
	public String getFortune() {
		// TODO Auto-generated method stub
		return null;
	}

}

在spring里面,如果没有在作用域声明的话,就会报错找不到指定的bean:


Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'com.luv2code.springdemo.FortuneService' available: expected single matching bean but found 4: databaseFortune,happyFortuneService,RESTFortuneService,randomService

因此,需要在使用的类作用域内指定调用的具体类:

public class TennisCoach implements Coach {
	@Autowired
	@Qualifier("databaseFortune")
	...
	}

DatabaseService
Reference:

Core Technologies about Spring:Spring Core Technologies

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值