1 首先建立一个接口 UserDao并在其中定义一个方法
public interface UserDao {
public void run();
}
2 定义一个实现类
UserDaolmpl.class连接上面的接口
public class UserDaolimp implements UserDao{
@Override
public void run() {
// TODO Auto-generated method stub
System.out.println("持久层");
}
}
3 建立一个配置文件.xml
4 建一个测试类
public class Test {
public static void main(String[] args) {
//加载配置("application.xml" 初始化容器
ApplicationContext ac=new ClassPathXmlApplicationContext("application.xml");
//获取制定的Bean ("userDao"):bean 的id
//需要强转一下"(UserDao)
UserDao aa=(UserDao) ac.getBean("userDao");
//UserService aa=(UserService) ac.getBean("UseServicelipm");
aa.run();
}
}
二 服务类 依赖注入
1 首先建立一个接口 UserService并在其中定义一个方法
public interface UserService {
public void run();
}
2 定义一个实现类
UserServicelmpl.class连接上面的接口
public class UserServicelmpl implements UserService{
//Service 想调用 Dao的方法
public UserDao userdaop;
//设置set为了依赖注入
public void setUserdaop(UserDao userdaop) {
this.userdaop = userdaop;
}
@Override
public void run() {
// TODO Auto-generated method stub
System.out.println("业务层");
userdaop.run();
}
}
3 建立一个配置文件.xml
4测试
public class Test {
public static void main(String[] args) {
//加载配置("application.xml" 初始化容器
ApplicationContext ac=new ClassPathXmlApplicationContext("application.xml");
//获取制定的Bean ("userDao"):bean 的id
//需要强转一下"(UserDao)
UserService aa=(UserService) ac.getBean("userServicelmpl");
//UserService aa=(UserService) ac.getBean("UseServicelipm");
aa.run();
}
}
二 Spring 中的Bean
(一)无参实例化
1 建立一个Bean类
//空类默认继承了 Object类 2 有一个无参的构造方法
public class Bean1 {
}
2建立Bean.xml
3建立一个Test类
public class Test {
public static void main(String[] args) {
//1.加载xml配置文件,初始化容器
String xmlPath="com/qingchneg/ssm/Bean1.xml";
ApplicationContext ac=new ClassPathXmlApplicationContext(xmlPath);
//通过容器获取实例对象
Bean1 bean1=(Bean1) ac.getBean("bean1");
System.out.println(bean1.toString());
}
}
(二)静态工厂
public class Bean2 {
}
public class MYBean2Factory {
public static Bean2 creatBean2() {
return new Bean2();
}
}
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test1 {
public static void main(String[] args) {
//1.加载xml配置文件,初始化容器
String xmlPath="com/qcby/factory/Bean2.xml";
ApplicationContext ac=new ClassPathXmlApplicationContext(xmlPath);
//通过容器获取实例对象
Bean2 bean2=(Bean2) ac.getBean("bean2");
System.out.println(bean2.toString());
}
}
非静态方法
public class Bean3 {
}
public class Bean3Factory {
public Bean3 creatBean3() {
return new Bean3();
}
}
public class Test3 {
public static void main(String[] args) {
//1.加载xml配置文件,初始化容器
String xmlPath="com/qingchneg/ssm/factory/Bean3.xml";
ApplicationContext ac=new ClassPathXmlApplicationContext(xmlPath);
//通过容器获取实例对象
Bean3 bean3=(Bean3) ac.getBean("bean3");
System.out.println(bean3.toString());
}
}
作用域
装配方式
基于Annotation的装配
UserDao和UserDaoImpl
public interface UserDao {
void save()
;
}
import org.springframework.stereotype.Repository;
/*相当于创建了一个id为UserDao的Bean*/
@Repository("userDao")
/*如果上面不写名字则默认的是 开头字母小写的类名*/
public class UserDaolmpl implements UserDao{
@Override
public void save() {
// TODO Auto-generated method stub
System.out.println("ddad");
}
}
UerSerice和UerSericeImpl
package annotation;
public interface UserService {
void save ()
;
package annotation;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
@Service("userservice")
public class UserServiceimpl implements UserService{
/*注入userdao的对象 name 是在userDao中创建的Bean的id*/
@Resource(name="userDao")
private UserDao userDao;
@Override
public void save() {
// TODO Auto-generated method stub
this.userDao.save();
}
usercontrol
import javax.annotation.Resource;
import javax.jws.soap.SOAPBinding.Use;
import org.springframework.stereotype.Controller;
@Controller("userController")
public class UsrContorl {
@Resource(name="userservice")
private UserService userService;
public void save() {
this.userService.save();
System.out.println("usercol de save");
}
}
xml
测试类
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test7 {
public static void main(String[] args) {
//1.加载xml配置文件,初始化容器
String xmlPath="annotation/Bean6.xml";
ApplicationContext ac=new ClassPathXmlApplicationContext(xmlPath);
//通过容器获取实例对象
UsrContorl user1=(UsrContorl) ac.getBean("userController");
user1.save();
}
}