Spring的IOC容器例子1

com.fyd.user.service.IUserService

package com.fyd.user.service;

public interface IUserService {
    public void say();
}

com.fyd.user.service.UserServiceImpl

package com.fyd.user.service;

import org.springframework.stereotype.Component;

@Component(value = "userService3")
public class UserServiceImpl implements IUserService{

    public void say() {
        System.out.println("床前明月光,地上鞋两双");
    }
}

com.fyd.config.SpringConfig

package com.fyd.config;

import com.fyd.user.service.IUserService;
import com.fyd.user.service.UserServiceImpl;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan(basePackages = "com.fyd")
public class SpringConfig {

    @Bean("userService4")
    public IUserService getUserService(){
        IUserService iUserService = new UserServiceImpl();
        return iUserService;
    }

}

User.xml

<?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 id="userService" class="com.fyd.user.service.UserServiceImpl"></bean>

</beans>

ApplicationContext.xml

<?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 https://www.springframework.org/schema/context/spring-context.xsd">

    <import resource="User.xml"></import>

    <context:component-scan base-package="com.fyd"></context:component-scan>
</beans>

测试代码:

package com.fyd.main;

import com.fyd.config.SpringConfig;
import com.fyd.user.service.UserServiceImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class Main {

	public static void main(String[] args) {
//		System.out.println("hello world");
//		System.out.println("我是一只小小小小鸟hello world");
//		System.out.println(StringUtils.upperCase("hello world"));

		//原生new
		UserServiceImpl userService1 = new UserServiceImpl();
		userService1.say();
		//xml方式
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("ApplicationContext.xml");
		UserServiceImpl userService2 = (UserServiceImpl) applicationContext.getBean("userService");
		userService2.say();
		//xml+注解方式 (原生注解)
		UserServiceImpl userService3 = (UserServiceImpl) applicationContext.getBean("userService3");
		userService3.say();
		//注解方式 (新注解)
		ApplicationContext applicationContext1 = new AnnotationConfigApplicationContext(SpringConfig.class);
		UserServiceImpl userService4 = (UserServiceImpl) applicationContext1.getBean("userService4");
		userService4.say();

	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值