Code for test article: Different Types of Bean Injection in Spring

This commit is contained in:
Radu Tamas 2017-10-24 22:48:54 +03:00
parent a5e2357033
commit 261a16988d
4 changed files with 61 additions and 0 deletions

View File

@ -0,0 +1,21 @@
package com.baeldung.dependencyinjectiontypes;
import org.springframework.beans.factory.annotation.Autowired;
public class Article {
private TextFormatter formatter;
public Article(TextFormatter formatter) {
this.formatter = formatter;
}
@Autowired
public void setTextFormatter(TextFormatter formatter) {
this.formatter = formatter;
}
public String format(String text) {
return formatter.format(text);
}
}

View File

@ -0,0 +1,16 @@
package com.baeldung.dependencyinjectiontypes;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class ArticleFormatter {
@SuppressWarnings("resource")
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("dependencyinjectiontypes-context.xml");
Article article = (Article) context.getBean("articleBean");
String formattedArticle = article.format("This is a text !");
System.out.print(formattedArticle);
}
}

View File

@ -0,0 +1,8 @@
package com.baeldung.dependencyinjectiontypes;
public class TextFormatter {
public String format(String text) {
return text.toUpperCase();
}
}

View File

@ -0,0 +1,16 @@
<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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd">
<context:annotation-config/>
<bean id="articleBean"
class="com.baeldung.dependencyinjectiontypes.Article">
<constructor-arg ref="textFormatterBean" />
</bean>
<bean id="textFormatterBean" class="com.baeldung.dependencyinjectiontypes.TextFormatter">
</bean>
</beans>