-
<?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:p ="http://www.springframework.org/schema/p" 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"> <!-- 开启注解--> <context:annotation-config/> <!-- 注解的位置--> <context:component-scan base-package="com.gq"/>
-
@Component("UserviceImpl") //这里配置的是id public class UserviceImpl implements UserService { private String name; public static void main(String[] args) { } }
-
public void t1() { ApplicationContext context = new ClassPathXmlApplicationContext("beans5.xml"); //Component没有配置id和name那么就通过类型获取 UserviceImpl uservice = context.getBean(UserviceImpl.class); UserviceImpl userviceImpl = (UserviceImpl) context.getBean("UserviceImpl"); }