Spring-創(chuàng)新互聯(lián)

Spring核心: Ioc (控制反轉(zhuǎn)):降低程序間的耦合(依賴關(guān)系)

站在用戶的角度思考問題,與客戶深入溝通,找到班戈網(wǎng)站設(shè)計(jì)與班戈網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個性化、用戶體驗(yàn)好的作品,建站類型包括:做網(wǎng)站、成都做網(wǎng)站、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、國際域名空間、虛擬主機(jī)、企業(yè)郵箱。業(yè)務(wù)覆蓋班戈地區(qū)。

spring配置:https://blog.csdn.net/qq_47959003/article/details/

Spring開發(fā)步驟:

  • 導(dǎo)入Spring開發(fā)的基本包坐標(biāo)
    –Bean標(biāo)簽配置
    ID:Bean實(shí)例在Spring容器中的唯一性標(biāo)識
    class:Bean的全限定名稱
    scope:singleton:默認(rèn)屬性,此時只有一個配置屬性,只加載一個配置文件 prototype:此時會加載多個配置文件,地址不一樣,運(yùn)行實(shí)例會運(yùn)行多次無參構(gòu)造

  • 創(chuàng)建Dao接口和實(shí)現(xiàn)類
  • 創(chuàng)建Spring核心配置文件
  • 在Spring配置文件中配置UserDaoImpl
  • 使用Spring的API獲得Bean實(shí)例

Spring重點(diǎn)配置:

id屬性:在容器中Bean實(shí)例的唯一標(biāo)識,不允許重復(fù)
	class屬性:要實(shí)例化的Bean的全限定名
	scope屬性:Bean的作用范圍,常用是Singleton(默認(rèn))和prototype
	標(biāo)簽:屬性注入
		name屬性:屬性名稱
		value屬性:注入的普通屬性值
		ref屬性:注入的對象引用值
		標(biāo)簽
		標(biāo)簽
		標(biāo)簽
	標(biāo)簽:屬性內(nèi)容同理bean標(biāo)簽:導(dǎo)入其他Spring分文件
Spring配置properties文件
//導(dǎo)入context加載路徑,復(fù)制beans,把beans改為context//class 表示引入的解析文件對象//name自定義,key為文件中的對應(yīng)對象

簡單實(shí)現(xiàn)案例:
采用mvc(model,vision,controll)三層架構(gòu)設(shè)計(jì)模式
設(shè)計(jì)Dao接口,Dao實(shí)現(xiàn)類接口,實(shí)現(xiàn)方法
Service層接口,service實(shí)現(xiàn)類接口,方法實(shí)現(xiàn)ApplicationContext對象實(shí)例化
controller實(shí)現(xiàn)Service對象方法,實(shí)例化對象實(shí)現(xiàn)方法

在這里插入圖片描述

dao層:

接口:
public interface UserDao {
        void run();
}

實(shí)現(xiàn)方法:
import com.example.spring_ioc_1.Dao.UserDao;

public class UserDaoImpl implements UserDao {
    @Override
    public void run() {
        System.out.println("run.....");
    }
}

Service層:

接口:
public interface UserService {
    public void save();
}

實(shí)現(xiàn)方法:
import com.example.spring_ioc_1.Dao.UserDao;
import com.example.spring_ioc_1.Service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class UserServiceImpl implements UserService {
    public void save() {
        ApplicationContext app=new ClassPathXmlApplicationContext("applicationContext.xml");
        UserDao userDao = (UserDao) app.getBean("userDao");
        userDao.run();

    }
}

web控制中心:

import com.example.spring_ioc_1.Service.UserService;
import com.example.spring_ioc_1.Service.impl.UserServiceImpl;


public class Controller {
    public static void main(String[] args) {
        UserService userService = new UserServiceImpl();
        userService.save();
    }
}

applicationContext.xml 配置:

Bean依賴注入:
采用注入方法就不用實(shí)現(xiàn)方法
在這里插入圖片描述
在這里插入圖片描述

– 構(gòu)造方法
在這里插入圖片描述

在這里插入圖片描述
name屬性指向構(gòu)造方法中的userDao,ref指向id中的userDao

– set方法

在這里插入圖片描述
在這里插入圖片描述
當(dāng)注入的屬性值較多時,使用p命名空間
在這里插入圖片描述

屬性注入

– 普通屬性注入:
在這里插入圖片描述
在這里插入圖片描述

– 集合注入:
創(chuàng)建集合
在這里插入圖片描述
map對象設(shè)置實(shí)現(xiàn)類
setter,getter,tostring方法
在這里插入圖片描述
bean編寫:
在這里插入圖片描述
在這里插入圖片描述

配置文件的引入:
當(dāng)業(yè)務(wù)較多時,為了區(qū)分業(yè)務(wù)模塊,可以進(jìn)行模塊的引入

在這里插入圖片描述
在這里插入圖片描述
Spring相應(yīng)api:
當(dāng)容器中的bean相同class屬性只有一種時,可以使用
在這里插入圖片描述
相同class出現(xiàn)多次時使用id指向唯一性:
在這里插入圖片描述

數(shù)據(jù)源開發(fā):

– c3p0數(shù)據(jù)源

com.mchangec3p00.9.5.2
@Test
    //測試c3p0數(shù)據(jù)源
    public void test1() throws PropertyVetoException, SQLException {
        ComboPooledDataSource source = new ComboPooledDataSource();
        source.setDriverClass("com.mysql.cj.jdbc.Driver");
        source.setJdbcUrl("jdbc:mysql://localhost:3306");
        source.setUser("sa");
        source.setPassword("123456");
        Connection connection = source.getConnection();
        System.out.println(connection);
        connection.close();
    }

– druid數(shù)據(jù)源

com.alibabadruid1.2.8
@Test
    //測試druid數(shù)據(jù)源
    public void test2() throws SQLException {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://localhost:3306");
        dataSource.setUsername("sa");
        dataSource.setPassword("123456");
        DruidPooledConnection connection = dataSource.getConnection();
        System.out.println(connection);
        connection.close();

    }

– 抽取properties文件進(jìn)行測試

properties文件:
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306
jdbc.user=sa
jdbc.password=123456
@Test
    //測試c3p0數(shù)據(jù)源(抽取properties文件)
    public void test3() throws PropertyVetoException, SQLException {
        //讀取配置文件
        ResourceBundle rb = ResourceBundle.getBundle("jdbc"); // 傳入文件地址
        String driver = rb.getString("jdbc.driver");
        String url = rb.getString("jdbc.url");
        String user = rb.getString("jdbc.user");
        String password = rb.getString("jdbc.password");
        //創(chuàng)建數(shù)據(jù)源對象,設(shè)置連接
        ComboPooledDataSource dataSource=new ComboPooledDataSource();
        dataSource.setDriverClass(driver);
        dataSource.setJdbcUrl(url);
        dataSource.setUser(user);
        dataSource.setPassword(password);
        Connection connection = dataSource.getConnection();
        System.out.println(connection);
        connection.close();
    }

    @Test
    //測試druid數(shù)據(jù)源(抽取properties文件)
    public void test4() throws SQLException {
        //讀取配置文件
        ResourceBundle rb = ResourceBundle.getBundle("jdbc"); // 傳入文件地址
        String driver = rb.getString("jdbc.driver");
        String url = rb.getString("jdbc.url");
        String user = rb.getString("jdbc.user");
        String password = rb.getString("jdbc.password");
        //創(chuàng)建數(shù)據(jù)源對象,設(shè)置連接
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(driver);
        dataSource.setUrl(url);
        dataSource.setUsername(user);
        dataSource.setPassword(password);
        DruidPooledConnection connection = dataSource.getConnection();
        System.out.println(connection);
        connection.close();

    }

spring加載數(shù)據(jù)源對象
根據(jù)抽取數(shù)據(jù)源對象的不同,需要注意class引用的對象

引入context命名空間

spring加載外部properties文件,并且配置對象
模板:

實(shí)現(xiàn):

@Test
    //測試spring容器產(chǎn)生數(shù)據(jù)源對象
    public void test5() throws SQLException {ApplicationContext app=new ClassPathXmlApplicationContext("applicationContext.xml");
        ComboPooledDataSource dataSource = (ComboPooledDataSource) app.getBean("dataSource");
        Connection connection = dataSource.getConnection();
        System.out.println(connection);
        connection.close();
    }

注解開發(fā):

在這里插入圖片描述
案例實(shí)現(xiàn):

配置Dao層
Service層
web層
以及applicationContext.xml文件
在這里插入圖片描述
applicationContext.xml文件配置組件庫的掃描,以便對注解開發(fā)的命令進(jìn)行實(shí)例化

Dao層

userDaoImpl文件:(語句替換)
//@Component("userDao")

userServiceIml文件:
在這里插入圖片描述
在這里插入圖片描述
@value標(biāo)簽
1:直接賦值普通屬性
2:進(jìn)行properties文件的抽?。╯pring需要配置命名空間)
在這里插入圖片描述
在這里插入圖片描述
注解標(biāo)注:
4種注解含義一樣,對于實(shí)現(xiàn)的層級不一樣
在這里插入圖片描述
初始方法,銷毀方法
銷毀方法需要在web層手動結(jié)束,初始方法在程序執(zhí)行開始執(zhí)行
在這里插入圖片描述
實(shí)現(xiàn)在Dao層
在這里插入圖片描述
web層關(guān)閉
在這里插入圖片描述
Spring新注解:
在這里插入圖片描述
在這里插入圖片描述
配置spring核心類,還可以根據(jù)應(yīng)用的不同建立不同的分文件
在這里插入圖片描述
spring核心類,該類調(diào)用

//標(biāo)志該類是Spring核心配置類
@Configuration

////@ComponentScan("spring_ioc_1")

//導(dǎo)入數(shù)據(jù)類對象到核心配置文件中來,當(dāng)需要導(dǎo)入多個時用{}
@Import({DataSourceConfiguration.class})
public class SpringConfiguration {}

數(shù)據(jù)類:

////@PropertySource("classpath:jdbc.properties")
public class DataSourceConfiguration {@Value("${jdbc.driver}")
    private String driver;
    @Value("${jdbc.url}")
    private String url;
    @Value("${jdbc.user}")
    private String user;
    @Value("${jdbc.password}")
    private String password;

    @Bean("dataSource")
    public ComboPooledDataSource getDataSource() throws PropertyVetoException { //Spring會將當(dāng)前的返回值以指定名稱存儲到Spring容器中
        ComboPooledDataSource source = new ComboPooledDataSource();
        source.setDriverClass(driver);
        source.setJdbcUrl(url);
        source.setUser(user);
        source.setPassword(password);
        return source;
    }
}

基于Spring的Test框架:

導(dǎo)入配置:
 		org.springframeworkspring-test5.0.2.RELEASE
定義框架
//基于spring框架的測試
@RunWith(SpringJUnit4ClassRunner.class)

//該方法為xml文檔測試,聲明Spring容器的架構(gòu)指定
//@ContextConfiguration("classpath:applicationContext.xml")
@ContextConfiguration(classes={SpringConfiguration.class})

進(jìn)行測試編寫
    //進(jìn)行字段注入
    @Autowired
    private UserService userService;

    @Autowired
    private DataSource dataSource;

    @Test
    public void test1() throws SQLException {userService.save();
        System.out.println(dataSource.getConnection());
    }

Spring集成web環(huán)境——基本三層架構(gòu)

業(yè)務(wù)層——>對象層——>Dao層
配置tomcat服務(wù)器
業(yè)務(wù)層:

public class userServlet extends HttpServlet {@Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {ApplicationContext app=new ClassPathXmlApplicationContext("applicationContext.xml");
        UserService userService = (UserService) app.getBean("userService");
        userService.save();
    }
}

web.xml配置

導(dǎo)入相應(yīng)的坐標(biāo)實(shí)現(xiàn)UserServletcom.example.spring_ioc_1.web.userServletUserServlet/userServlet

在這里插入圖片描述
在這里插入圖片描述
實(shí)現(xiàn)監(jiān)聽對象,減少引用上下文實(shí)例的實(shí)現(xiàn):

在這里插入圖片描述
在這里插入圖片描述
在這里插入圖片描述
spring架構(gòu)實(shí)現(xiàn)的應(yīng)用上下文架構(gòu):
不知道為啥用不了

org.springframework.web.context.ContextLoaderListenercontextConfigLocationclasspath:applicationContext.xml

在這里插入圖片描述

你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機(jī)房具備T級流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級服務(wù)器適合批量采購,新人活動首月15元起,快前往官網(wǎng)查看詳情吧

網(wǎng)站名稱:Spring-創(chuàng)新互聯(lián)
本文鏈接:http://bm7419.com/article10/ggggo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)建站、網(wǎng)頁設(shè)計(jì)公司外貿(mào)網(wǎng)站建設(shè)、網(wǎng)站改版、Google、ChatGPT

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

綿陽服務(wù)器托管