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();
}
}