Spring依赖注入 加包扫描的方式Spring注解注入(学习笔记)

Spring的核心   依赖注入   DI  切面编程 AOP

spring要引入的jar包有:

commons-logging-1.2
spring-beans-4.1.4
spring-context-4.1.4
spring-core-4.1.4
spring-expression-4.1.4

Spring依赖注入的原理是:Spring是个bean容器,从XML里面读配置,通过反射创建对象,装载到spring容器 (XML加反射)

spring根据bean构造器拿到对象属性

先创个实体类(符合javabean规范)
public class User {
    private int id;
    private String name;


    public User(){


    }
    public User(int id, String name) {
        this.id = id;
        this.name = name;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

在src目录右键XMLConfigurationFile创建个spring Config

       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">


   
       
       
       
   

在主函数中写:
public class Main {

    public static void main(String[] args) {
        //拿到spring上下文  就拿到spring的bean了
        ApplicationContext context=new ClassPathXmlApplicationContext("./com/dengweiquan/resource/application-context.xml");
        User user=(User) context.getBean("user");//从spring的容器里拿对象  这样就好用多了   提高代码重用性,就避免了用对象时就要new一个对象
        System.out.println(user.getId());//输出12345
        System.out.println(user.getName()); //输出laowang
        System.out.println(user.getClass()); //输出class com.dengweiquan.entity.User

    }
}

倘若不通过构造器获取对象属性,在application-context.xml里改成用property取对象属性


       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">
   
       
       
       
       
       

   

在主函数输出的结果也是一样的

spring根据bean拿到复杂对象属性

application-context.xml   :

       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">
 
       
       
       
       
       
   

   
       
       
       
   

再创建一个UserFather 示范类

public class UserFather {
    private String fatherName;
    private User user;
    public String getFatherName() {
        return fatherName;
    }
    public void setFatherName(String fatherName) {
        this.fatherName = fatherName;
    }
    public User getUser() {
        return user;
    }
    public void setUser(User user) {
        this.user = user;
    }
}
主函数写:
 //拿到spring上下文  就拿到spring的bean了
       ApplicationContext context=new ClassPathXmlApplicationContext("./com/dengweiquan/resource/application-context.xml");
        UserFather userFather=(UserFather) context.getBean(UserFather.class);
        System.out.println(userFather.getFatherName());//输出laodie

SSM框架知识点  mybatis控制访问数据库  springmvc控制请求  spring控制业务,容器

Spring的注解注入(@Autowired注入)   用包扫描的方式  :
加了@Service注解  经过了包扫描都加入了spring容器中
除了以上那些包 ,还要再引入
aopliance-1.0
spring-aop
aspectjweaver

新创一个UserService接口  再加个@Service注解
@Service
public interface UserService {
    public void say();

}

再创建一个UserServiceImpl类继承 UserService
@Service()
public class UserServiceImpl implements UserService {
    @Override
    public void say() {
     System.out.println("成功运行");
    }
}

加入包扫描要在application-context.xml的标签头里加下面三行代码
 http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd"
       xmlns:context="http://www.springframework.org/schema/context"

application-context.xml :

       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
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd"
       xmlns:context="http://www.springframework.org/schema/context"

>
   
       
       
       
       
       
   

   
       
       
       
   


再创建controller包  包中创建类UserController   作为例子用来控制事务
@Controller
public class UserController {

    @Autowired
    private UserService userService;

    public UserService getUserService() {
        return userService;
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }
}



主函数:
public class Main {

    public static void main(String[] args) {
        //拿到spring上下文  就拿到spring的bean了
        ApplicationContext context=new ClassPathXmlApplicationContext("./com/dengweiquan/resource/application-context.xml");
        //spring注解方式注入  MVC  model view controller
       // @Service  业务逻辑
        //@Component  公共组件
        //@Controller   控制用户请求  springMVC


        UserController userController=context.getBean(UserController.class);
         UserService service= userController.getUserService();
        service.say();     //输出成功运行
    }
}

编程运用了面向接口编程   这样程序的拓展性就很强
包目录结构:
Spring依赖注入 加包扫描的方式Spring注解注入(学习笔记)_第1张图片







      
    






你可能感兴趣的:(javaweb后端)