Java 向同一类的不同实例注入属性(Spring框架)
“如果需要,请将其标记为重复,但请在注释中留下解决方案链接。”Java 向同一类的不同实例注入属性(Spring框架),java,spring,dependency-injection,Java,Spring,Dependency Injection,“如果需要,请将其标记为重复,但请在注释中留下解决方案链接。” 我有一个类“Assignment22.Player.java”,我必须创建其中的5个实例并向它们注入属性。我可以通过在XML文件本身中硬编码将属性注入实例。但我希望这样做,以防将来有“更改”需求,我不必转到XML文件来更改属性。这可以通过使用一个/多个属性文件来实现 以下是我试图做的: 我为五个不同的实例创建了五个属性文件。但是,由于类是相同的,因此每个对象的属性名称都是相同的。所以看起来,我不能这样使用属性文件 以下是我尝试的xm
我有一个类“Assignment22.Player.java”,我必须创建其中的5个实例并向它们注入属性。我可以通过在XML文件本身中硬编码将属性注入实例。但我希望这样做,以防将来有“更改”需求,我不必转到XML文件来更改属性。这可以通过使用一个/多个属性文件来实现
以下是我试图做的:
我为五个不同的实例创建了五个属性文件。但是,由于类是相同的,因此每个对象的属性名称都是相同的。所以看起来,我不能这样使用属性文件
以下是我尝试的xml代码:
<!-- country bean -->
<bean id = "country1" class="Assignmetn22.Country">
<property name= "countryId" value="${countryId}"></property>
<property name= "countryName" value="${countryName}"></property>
</bean>
<bean id = "country2" class="Assignmetn22.Country">
<!-- since the properties' names are same there won't any effect if assign them different values
and use them for different beans.
-->
</bean>
<bean id = "player1" class="Assignment22.Player">
<property name="playerId" value="${playerId}"></property>
<property name="playerName" value="${playrName}"></property>
<property name="country" ref="country1"></property>
</bean>
<bean id = "player2" class="Assignment22.Player">
<!-- same properties' names for other bean -->
<!-- since the properties' names are same there won't any effect if assign them different values
and use them for different beans.
-->
<!-- <property name="playerId" value="${playerId}"></property>
<property name="playerName" value="${playrName}"></property>
<property name="country" ref="country2"></property>
-->
</bean>
<!--loading the properties file-->
<bean id= "placeholderConfig1" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:p1.properties"></property>
</bean>
<bean id= "placeholderConfig2" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:p2.properties"></property>
</bean>
<bean id= "placeholderConfig3" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:p3.properties"></property>
</bean>
<bean id= "placeholderConfig4" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:p4.properties"></property>
</bean>
<bean id= "placeholderConfig5" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:p5.properties"></property>
</bean>
我需要知道如何将不同的属性值动态地注入到相同引用类型(具有相同属性名称)的不同bean中。我不想用XML硬编码它们。有什么java方法吗?请解释。您可以使用Spring@Valuebean中的注释。创建的新实例时,Spring将为您填充属性值
public class Player
{
@Value("${playerName}")
private String playerName;
}
如果您希望坚持使用XML配置而不使用注释,请创建一个包含您的属性的bean,并将其连接到您的Player bean中
在PlayerProperty上使用scope=“prototype”为每个玩家创建一个实例。这样玩家就不会共享这些属性
使用scope=“singleton”(这是默认设置)让播放器实例共享PlayerProperties Bean
public class Player
{
PlayerProperties props;
}
public class PlayerProperties
{
String playerName;
//getter,setter,...
}
<bean id = "player2" class="Player">
<property name="props" ref="playerProperties"></property>
...
</bean>
<bean id = "playerProperties" class="PlayerProperties" scope="prototype">
<property name="playerName" value="${playrName}"></property>
...
</bean>
公共类播放器
{
玩家属性道具;
}
公共类PlayerProperty
{
弦乐演奏者姓名;
//getter,setter,。。。
}
...
...
应用程序类别:
@ComponentScan
@EnableAutoConfiguration
@EnableConfigurationProperties
@EnableAsync
@EnableScheduling
@Configuration
@SpringBootApplication
//use players.properties
@PropertySource("classpath:players.properties")
public class InstancePropsApp {
public static void main(String[] args) {
SpringApplication.run(InstancePropsApp.class, args);
}
//Create 3 Player Beans (could be done in XML-Config, too)
@Bean Player player1() {return new Player();}
@Bean Player player2() {return new Player();}
@Bean Player player3() {return new Player();}
@PostConstruct
public void init()
{
//Output values after init.
player1().echoName();
player2().echoName();
player3().echoName();
}
}
球员级别
@Component
@Scope("prototype")
//Make bean aware of it's name to use bean name as property prefix
public class Player implements BeanNameAware {
@Autowired
private ConfigurableEnvironment environment;
private String name;
public void setName(String name) {
this.name = name;
}
private String beanName;
@PostConstruct
public void init() {
//init this beans properties from "beanName.xxx" values. e.g.
//player1.name and player2.name ans so on
new RelaxedDataBinder(this, beanName).bind(new
PropertySourcesPropertyValues(environment.getPropertySources()));
}
//We use our very own bean name as prefix to load properties per
//prototype instance
public void setBeanName(String beanName) {
this.beanName = beanName;
}
public void echoName()
{
System.out.println(beanName + " : " + name);
}
}
players.properties:
player1.name=max
player2.name=theo
player3.name=karren
将打印
player1 : max
player2 : theo
player3 : karren
在给定的属性文件中,将存在唯一的属性=值对。对于给定的类,五个对象将具有相同的属性名(实例变量)。例如-'类OBJ obj1,obj2;obj1.name=“坟墓”;obj2.name=“zerobyzero””。问题是如何分别向obj1和obj2的name属性注入值?我不想用XML硬编码。建议我一些java方式或使用属性文件。