Spring. Inversion of Control with Annotations.
<?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.example.demo"/>
</beans>
package com.example.demo;
public interface Coach {
public String getDailyWorkout();
}package com.example.demo;
import org.springframework.stereotype.Component;
@Component
public class TennisCoach implements Coach{
@Override
public String getDailyWorkout() {
return "Practice your backhand volley";
}
}package com.example.demo;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class AnnotationDemoApp {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Coach coach = context.getBean("tennisCoach", Coach.class);
System.out.println(coach.getDailyWorkout());
context.close();
}
}
Комментарии
Отправить комментарий