接口
package com.guigu.g_annotion.a_ioc;
public interface BookService {
public void add();
}
==========================================================
实现类
package com.guigu.g_annotion.a_ioc;
import org.springframework.stereotype.Component;
@Component("bookService")
public class BookServiceImpl implements BookService {
@Override
public void add() {
System.out.println("ahdd");
}
}
============================================================
测试类
package com.guigu.g_annotion.a_ioc;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class iocTest {
@Test
public void demo1(){
ApplicationContext a = new ClassPathXmlApplicationContext("com/guigu/g_annotion/a_ioc/beans.xml");
BookService bean = a.getBean("bookService",BookService.class);
bean.add();
}
}
===========================================================
配置文件
<?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
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 组件扫描,扫描含有注解的类 -->
<context:component-scan base-package="*"></context:component-scan>
</beans>