spring笔记-01 Ioc

spring是开源框架,容器,非入侵式。控制反转Ioc,面向切面Aop,支持事务。

控制反转Ioc

原始的方法

原始的JAVA程序是手动new Object,比如原始DAO代码的实现,一般有几个类:

  1. DAO接口
    public interface UserDao {
    public void getUser();
    }

  2. DAO实现类
    public class UserDaoImpl implements UserDao {
    @Override
    public void getUser() {...

  3. service接口
    public interface UserService {
    public void getUser();
    }

  4. Service的实现类
    public class UserServiceImpl implements UserService {
    private UserDao userDao = new UserDaoImpl();
    @Override
    public void getUser() {
    userDao.getUser();
    }
    }

  5. usege
    @Test
    public void test(){
    UserService service = new UserServiceImpl();
    service.getUser();
    }

不同的sql 则要编写不同的UserDaoImpl ,麻烦。

2改进:

使用一个set方法设置userDao 接口!
public class UserServiceImpl implements UserService {
private UserDao userDao;
// 利用set实现
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
@Override
public void getUser() {
userDao.getUser();
}
}

使用:
public void test(){
UserServiceImpl service = new UserServiceImpl();
service.setUserDao( new UserDaoMySqlImpl() );
service.getUser();
//那我们现在又想用Oracle去实现呢
service.setUserDao( new UserDaoOracleImpl() );
service.getUser();
}
以前所有东西都是由程序去进行控制创建 , 而现在是由我们自行控制创建对象 , 把 主动权交给了调用者 . 程序不用去管怎么创建,怎么实现了,它只负责提供一个接口 .这是spring的原型!使用spring后,就可以把这些交给Spring进行对象的统一创建和管理。因此Spring可以看成一个大容器。

3控制反转IoC(Inversion of Control)

控制反转IoC,是一种设计思想,DI( 依赖注入) 是实现IoC 的一种方法.

两种方法实现Ioc

1) xml cfg

<import resource="services.xml"/>
<import resource="resources/messageSource.xml"/>
<import resource="/resources/themeSource.xml"/>

<bean id="bean1" class="...">  
    <!-- collaborators and configuration for this bean go here -->
</bean>

<bean id="bean2" class="...">
    <!-- collaborators and configuration for this bean go here -->
</bean>

// create and configure beans
ApplicationContext context = new ClassPathXmlApplicationContext("services.xml", "daos.xml");
// retrieve configured instance
PetStoreService service = context.getBean("petStore", PetStoreService.class);
// use configured instance
List userList = service.getUsernameList();

团队的合作通过import来实现 .

(1) 实体类User.java:

public class User implements java.io.Serializable {
private String name;
public User() {
System.out.println("user无参构造方法");
}
public String getName() { return name; }
public void setName(String name) {this.name = name;}
public void show(){
System.out.println("name="+ name );
}
}

(2) 、beans.xml

3、测试类

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
@Test
public void test(){
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
//在执行getBean的时候, user已经创建好了 , 通过无参构造
User user = (User) context.getBean("user");
//User user = context.getBean("user",User.class);
//调用对象的方法 .
user.show();
}
结果可以发现,在调用show方法之前,User对象已经通过无参构造初始化了

2) annotation-based cfg

@Configuration, @Bean, @Import, and @DependsOn annotations.

posted @ 2022-02-05 01:46  wqheng6  阅读(25)  评论(0)    收藏  举报