码迷,mamicode.com
首页 > 数据库 > 详细

SQL语言基础

时间:2020-08-08 21:23:14      阅读:82      评论:0      收藏:0      [点我收藏+]

标签:efi   不同的   修改   不同   tran   查询   工作   创建数据库   grant   

依赖注入(dependency injection):是Spring框架核心ioc(inversion of control)的具体实现

注入bean

通过set的方式

package com.example.demo.service.impl;

import com.example.demo.dao.UserDao;
import com.example.demo.service.UserService;

public class UserServiceImpl implements UserService {

    private UserDao userDao;

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    @Override
    public void save() {
        userDao.save();
    }
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       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">

<bean id="userDao" class="com.example.demo.dao.UserDao" />

<bean id="userServiceImpl" class="com.example.demo.service.impl.UserServiceImpl">
    <property name="userDao" ref="userDao"/>
</bean>
    
</beans>

备注:

  1. name="userDao"中的userDao是根据setUserDao的UserDao(首字母变小写)来的

通过p命名空间的方式

p命名空间注入的本质就是set注入

<?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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

<bean id="userDao" class="com.example.demo.dao.UserDao" />

<bean id="userServiceImpl" class="com.example.demo.service.impl.UserServiceImpl" p:userDao-ref="userDao"/>
 
</beans>

备注:

  1. userDao-ref中的userDao是根据setUserDao的UserDao(首字母变小写)来的

通过构造器的方式

package com.example.demo.service.impl;

import com.example.demo.dao.UserDao;
import com.example.demo.service.UserService;

public class UserServiceImpl implements UserService {

    private UserDao userDao;

    public UserServiceImpl(UserDao userDao) {
        this.userDao = userDao;
    }

    @Override
    public void save() {
        userDao.save();
    }
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       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">

<bean id="userDao" class="com.example.demo.dao.UserDao" />

    <bean id="userServiceImpl" class="com.example.demo.service.impl.UserServiceImpl">
        <constructor-arg name="userDao" ref="userDao"/>
    </bean>

</beans>

备注:

  1. name="userDao"中的userDao是根据public UserServiceImpl(UserDao userDao)(构造器参数名)来的

注入普通数据类型,集合等

SQL语言基础

标签:efi   不同的   修改   不同   tran   查询   工作   创建数据库   grant   

原文地址:https://www.cnblogs.com/ccku/p/13460310.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!