Spring 自动依赖注入

来源:互联网 发布:网络用语rz是什么意思 编辑:程序博客网 时间:2024/06/05 23:48

– Start
事实上,Spring 还支持自动依赖注入。

基于 XML 的自动依赖注入

package shangbo.spring.example31;public interface MessageService {    String getMessage();}
package shangbo.spring.example31;public class MessageServiceImpl implements MessageService {    public String getMessage() {        return "Hello World";    }}
package shangbo.spring.example31;public class MessagePrinter {    private MessageService service;    public void setService(MessageService service) {        this.service = service;    }    public void printMessage() {        System.out.println(service.getMessage());    }}
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xsi:schemaLocation="http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans.xsd">    <bean class="shangbo.spring.example31.MessageServiceImpl"/>    <!--         autowire="no" 表示手动注入        autowire="byType" 表示根据类型,通过 setter 方法自动注入        autowire="constructor" 表示根据类型,通过构造器方法自动注入        autowire="byName" 表示根据名字自动注入    -->    <bean class="shangbo.spring.example31.MessagePrinter" autowire="byType"/></beans>
package shangbo.spring.example31;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class App {    public static void main(String[] args) {        // 实例化 Spring IoC 容器        ApplicationContext context = new ClassPathXmlApplicationContext("example.xml", MessagePrinter.class);        // 从容器中获得 MessagePrinter 的实例        MessagePrinter printer = context.getBean(MessagePrinter.class);        // 使用对象        printer.printMessage();    }}

除此之外,我们还可以全局设定自动注入方式。

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xsi:schemaLocation="http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans.xsd"        default-autowire="byType">    <!--         全局设定自动注入方式    -->    <bean class="shangbo.spring.example32.MessageServiceImpl"/>    <bean class="shangbo.spring.example32.MessagePrinter"/></beans>

基于注解的自动依赖注入

package shangbo.spring.example33;public interface MessageService {    String getMessage();}
package shangbo.spring.example33;public class MessageServiceImpl implements MessageService {    public String getMessage() {        return "Hello World";    }}
package shangbo.spring.example33;import org.springframework.beans.factory.annotation.Autowired;public class MessagePrinter {    private MessageService service;    // 自动注入    @Autowired    public void setService(MessageService service) {        this.service = service;    }    public void printMessage() {        System.out.println(service.getMessage());    }}
package shangbo.spring.example33;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;@Configurationpublic class AppConfig {    @Bean    public MessageService messageService() {        return new MessageServiceImpl();    }    @Bean    public MessagePrinter messagePrinter() {        return new MessagePrinter();    }}
package shangbo.spring.example33;import org.springframework.context.ApplicationContext;import org.springframework.context.annotation.AnnotationConfigApplicationContext;public class App {    public static void main( String[] args )    {        // 实例化 Spring IoC 容器,也可以一次读取多个Java配置文件        ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);        // 从容器中获得 MessagePrinter 的实例        MessagePrinter printer = context.getBean(MessagePrinter.class);        // 使用对象        printer.printMessage();    }}

更多参见:Spring Framework 精萃
– 声 明:转载请注明出处
– Last Updated on 2017-05-23
– Written by ShangBo on 2017-05-23
– End

原创粉丝点击