File tree Expand file tree Collapse file tree
java/com/baeldung/beaninjection Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ package com .baeldung .beaninjection ;
2+
3+ import org .springframework .context .annotation .Bean ;
4+ import org .springframework .context .annotation .ComponentScan ;
5+ import org .springframework .context .annotation .Configuration ;
6+
7+ import com .baeldung .beaninjection .domain .ConsoleLogger ;
8+ import com .baeldung .beaninjection .domain .Logger ;
9+
10+ @ Configuration
11+ @ ComponentScan ("com.baeldung.beaninjection" )
12+ public class BeanConfiguration {
13+
14+ @ Bean
15+ public Logger logger () {
16+ return new ConsoleLogger ();
17+ }
18+
19+ @ Bean
20+ public int maxChars () {
21+ return 15 ;
22+ }
23+
24+ @ Bean
25+ public String messagePrefix (){
26+ return "JConfig: " ;
27+ }
28+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .beaninjection ;
2+
3+ import org .springframework .context .ApplicationContext ;
4+ import org .springframework .context .ConfigurableApplicationContext ;
5+ import org .springframework .context .annotation .AnnotationConfigApplicationContext ;
6+ import org .springframework .context .support .ClassPathXmlApplicationContext ;
7+
8+ import com .baeldung .beaninjection .domain .LoggingHelper ;
9+
10+ public class MainApp {
11+ public static void main (String [] args ) {
12+
13+ LoggingHelper logHelper = loadFromJavaConfig ();
14+ logHelper .logMessage ("This is a log message" );
15+
16+ logHelper = loadFromXmlConfig ();
17+ logHelper .logMessage ("This is another log message" );
18+ }
19+
20+ private static LoggingHelper loadFromJavaConfig () {
21+ ApplicationContext context = new AnnotationConfigApplicationContext (BeanConfiguration .class );
22+
23+ LoggingHelper loggingHelper = context .getBean (LoggingHelper .class );
24+ ((ConfigurableApplicationContext )context ).close ();
25+
26+ return loggingHelper ;
27+ }
28+
29+ private static LoggingHelper loadFromXmlConfig () {
30+ ApplicationContext context = new ClassPathXmlApplicationContext ("beaninjection.xml" );
31+
32+ LoggingHelper loggingHelper = context .getBean (LoggingHelper .class );
33+ ((ConfigurableApplicationContext )context ).close ();
34+
35+ return loggingHelper ;
36+ }
37+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .beaninjection .domain ;
2+
3+ import org .springframework .beans .factory .annotation .Autowired ;
4+
5+ public class ConsoleLogger implements Logger {
6+
7+ private String messagePrefix ;
8+ private static final String DEFAULT_MESSAGE_PREFIX = "LOG: " ;
9+
10+ @ Override
11+ public void logMessage (String message , int maxChars ) {
12+ System .out .println (getMessagePrefix () + message .substring (0 , maxChars ));
13+ }
14+
15+ private String getMessagePrefix (){
16+ return this .messagePrefix == null ? DEFAULT_MESSAGE_PREFIX : messagePrefix ;
17+ }
18+
19+ @ Autowired
20+ public void setMessagePrefix (String messagePrefix ){
21+ this .messagePrefix = messagePrefix ;
22+ }
23+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .beaninjection .domain ;
2+
3+ public interface Logger {
4+
5+ public void logMessage (String message , int maxChars );
6+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .beaninjection .domain ;
2+
3+ import org .springframework .beans .factory .annotation .Autowired ;
4+ import org .springframework .stereotype .Component ;
5+
6+ @ Component
7+ public class LoggingHelper {
8+ private Logger logger ;
9+ private int maxChars ;
10+
11+ @ Autowired
12+ public LoggingHelper (Logger logger , int maxChars ) {
13+ this .logger = logger ;
14+ this .maxChars = maxChars ;
15+ }
16+
17+ public void logMessage (String message ) {
18+ logger .logMessage (message , maxChars );
19+ }
20+ }
Original file line number Diff line number Diff line change 1+ <beans xmlns =" http://www.springframework.org/schema/beans"
2+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
3+ xsi : schemaLocation =" http://www.springframework.org/schema/beans
4+ http://www.springframework.org/schema/beans/spring-beans-3.0.xsd" >
5+
6+ <bean id =" logHelper" class =" com.baeldung.beaninjection.domain.LoggingHelper" >
7+ <constructor-arg index =" 0" ref =" logger" />
8+ <constructor-arg index =" 1" type =" int" value =" 20" />
9+ </bean >
10+
11+ <bean id =" logger" class =" com.baeldung.beaninjection.domain.ConsoleLogger" >
12+ <property name =" messagePrefix" value =" XMLConfig: " ></property >
13+ </bean >
14+
15+ </beans >
You can’t perform that action at this time.
0 commit comments