The two ways in which spring creates a policy pattern (XML configuration and pure annotations) _spring

Source: Internet
Author: User
Tags stub

The strategy pattern is a good design pattern, and spring implements the policy pattern to simplify the code flow:

XML and pure annotations can be implemented, but the pure annotation is more convenient, no configuration, more convenient: First look at the annotation way:

The public interface is implemented

Package org.gyy.strategy.iface;

Public interface Icontextstrategy {

	void Say (String name);
	
}
Implementation class:
Package Org.gyy.strategy.impl;

Import Org.gyy.strategy.iface.IContextStrategy;
Import org.springframework.stereotype.Component;

@Component public
class Alicontextstrategy implements Icontextstrategy {

	@Override public
	void Say (String Name) {
		System.out.println ("Alibaba Group Welcomes You:" +name);
	}



Package Org.gyy.strategy.impl;

Import Org.gyy.strategy.iface.IContextStrategy;
Import org.springframework.stereotype.Component;
@Component public
class Bducontextstrategy implements icontextstrategy{

	@Override public
	Void Say ( String name {
		//TODO auto-generated method stub
		System.out.println ("Welcome to Baidu Group:" +name);
	}

	


Package Org.gyy.strategy.impl;

Import Org.gyy.strategy.iface.IContextStrategy;
Import org.springframework.stereotype.Component;
@Component public
class Tencontextstrategy implements Icontextstrategy {

	@Override public
	void Say (String Name) {
		//TODO auto-generated method stub
		System.out.println ("Welcome to Tencent Group:" +name);
	}



Factory: Spring when the map is injected, the class masterpiece is key

Package org.gyy.strategy;

Import Java.util.HashMap;
Import Java.util.Map;

Import Javax.annotation.Resource;

Import Org.gyy.strategy.iface.IContextStrategy;
Import org.springframework.beans.factory.annotation.Autowired;
Import org.springframework.stereotype.Component;
@Component public
class Contextstrategyfactory {
	@Autowired
	private   map<string, icontextstrategy> contextstrategy = new hashmap<string,icontextstrategy> ();

	Public map<string, Icontextstrategy> Getcontextstrategy () {return
		contextstrategy;
	}

	public void Setcontextstrategy (map<string, icontextstrategy> contextstrategy) {
		this.contextstrategy = contextstrategy;
	}

	Public icontextstrategy dostrategy (String type) {return
		
		this.contextStrategy.get (type);
	}
	
	
}
Test:

Package org.gyy.strategy;

Import Javax.annotation.Resource;

Import Org.gyy.baseTest.BaseJunit4Test;
Import Org.gyy.strategy.iface.IContextStrategy;
Import Org.junit.Test;

public class Strategytest extends Basejunit4test {
	@Resource
	private contextstrategyfactory strategyfactory;
	
	@Test public
	void Teststrategy () {
		Icontextstrategy dostrategy = Strategyfactory.dostrategy (" Alicontextstrategy ");
		Dostrategy.say ("John");
	}


Here is the basic implementation, if there are complex business requirements, can be combined to implement specific methods,

The following is the configuration of XML, not adding an implementation class to the map plus a key value pair, the special trouble:

<bean id= "Contextstrategyfactory" class= "org.gyy.strategy.ContextStrategyFactory" >
       <property name= " Contextstrategy ">
       <map>
       	<entry key=" 1 "value-ref=" Alicontextstrategy "/> <entry key=
       	"2" value-ref= "Bducontextstrategy"/>
       	<entry key= "3" value-ref= "Tencontextstrategy"/>
       </map >
       </property>
   </bean>
   <bean id= "Alicontextstrategy" class= " Org.gyy.strategy.impl.ALIContextStrategy "/>
   <bean id=" Bducontextstrategy "class=" Org.gyy.strategy.impl.BDUContextStrategy "/>
   <bean id=" Tencontextstrategy "class=" Org.gyy.strategy.impl.TENContextStrategy "/>



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.