Spring--Ioc注入式编程
一,Set注入
1.根据项目构建01-构建项目
---导入spring jar包
---创建 com.liuboss.dao comliuboss.service层
com.liuboss.dao 层 创建UserDao接口
package com.liuboss.dao;
public interface UserDao {
public void save();
}
com.liuboss.dao.Impl 层 创建UserDaoImpl实现类
实UserDao的接口
package com.liuboss.dao.impl;
import com.liuboss.dao.UserDao;
public class UserDaoImpl implements UserDao {
public void save() {
System.out.println("User Dao running.......");
}
}
创建 service层 创建 UserService接口
package com.liuboss.service;
public interface UserService {
public void save();
}
---包下创建impl包 UserServiceImpl类
实现UserService接口
package com.liuboss.service.Impl;
import com.liuboss.dao.UserDao;
import com.liuboss.dao.impl.UserDaoImpl;
import com.liuboss.service.UserService;
public class UserServiceImlp implements UserService {
public void userServiceimpl(){
System.out.println("类已经被加载......");
}
public void init(){
System.out.println("初始化方法.....");
}
public void destory(){
System.out.println("销毁方法.....");
}
private String str;
public void setStr(String str) {
this.str = str;
}
private int num;
public void setNum(int num) {
this.num = num;
}
//声明私有变量
private UserDao userDao;
//实现私有比变量的set方法
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public void save() {
System.out.println("User Service running..........."+num+" "+str);
userDao.save();
}
}
---在resources配置文件下进行资源配置
---创建applicationContext.xml
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!--scope="singleton"创建单例模式 创建多例模式 scope="prototype"-->
<!--<bean id="userService3" scope="singleton" init-method="init" destroy-method="destory" class="com.liuboss.service.Impl.UserServiceImlp"/>-->
<!--实现资源注入-->
<bean id="userService" class="com.liuboss.service.Impl.UserServiceImlp">
<!--name为 注入的名称 ref为注入资源的资源id-->
<property name="userDao" ref="userDao"/>
<property name="num" value="666"/>
<property name="str" value="永乐大帝"/>
</bean>
<!--想要实现资源注入 要进行资源的声明 保证资源受到spring资源控制-->
<bean id="userDao" class="com.liuboss.dao.impl.UserDaoImpl"/>
</beans>
--service包下创建实现类
UserApp
实现其中的方法
package com.liuboss.service;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class UserApp {
public static void main(String[] args) {
ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService = (UserService) app.getBean("userService");
userService.save();
}
}
实现输出
重点讲解 :
package com.liuboss.service.Impl;
import com.liuboss.dao.UserDao;
import com.liuboss.dao.impl.UserDaoImpl;
import com.liuboss.service.UserService;
public class UserServiceImlp implements UserService {
public void userServiceimpl(){
System.out.println("类已经被加载......");
}
public void init(){
System.out.println("初始化方法.....");
}
public void destory(){
System.out.println("销毁方法.....");
}
private String str;
public void setStr(String str) {
this.str = str;
}
private int num;
public void setNum(int num) {
this.num = num;
}
//声明私有变量
private UserDao userDao;
//实现私有比变量的set方法
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public void save() {
System.out.println("User Service running..........."+num+" "+str);
userDao.save();
}
}
实现值的注入 只需要在service中 声明私有变量 实现set方法
之后再配置文件中进行注入。
//声明私有变量
private UserDao userDao;
//实现私有比变量的set方法
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
<!--实现资源注入-->
<bean id="userService" class="com.liuboss.service.Impl.UserServiceImlp">
<!--name为 注入的名称 ref为注入资源的资源id-->
<property name="userDao" ref="userDao"/>
<property name="num" value="666"/>
<property name="str" value="永乐大帝"/>
</bean>
<!--想要实现资源注入 要进行资源的声明 保证资源受到spring资源控制-->
<bean id="userDao" class="com.liuboss.dao.impl.UserDaoImpl"/>
注意:
其中index为标记注入值的顺序,要将顺序进行标记才能好着正常识别
<bean id="userDao" class="com.liuboss.dao.Impl.UserDaoImpl"/>
<bean id="userService" class="com.liuboss.service.Impl.UserServiceImpl">
<constructor-arg index="0" ref="userDao"/>
<constructor-arg index="1" value="秦王嬴政"/>
</bean>
重点:
spring报错方式解决办法,spring报错输出格式过长
看错误的方式 要从最后开始看 从后往前看 每个代码块的第一行为报错方式
然后逐个解决
---数据类型注入方式
1.创建BookDao接口
package com.liuboss.book;
public interface BookDao {
public void save();
}
创建实现类BookDaoImpl实现BookDao接口
package com.liuboss.book.Impl;
import com.liuboss.book.BookDao;
import lombok.Data;
import java.util.*;
@Data
public class BookDaoImpl implements BookDao {
private ArrayList arrayList;
public void setArrayList(ArrayList arrayList) {
this.arrayList = arrayList;
}
private Properties properties;
private int[] arr;
private HashSet hashSet;
private HashMap hashMap;
public void setProperties(Properties properties) {
this.properties = properties;
}
public void setArr(int[] arr) {
this.arr = arr;
}
public void setHashSet(HashSet hashSet) {
this.hashSet = hashSet;
}
public void setHashMap(HashMap hashMap) {
this.hashMap = hashMap;
}
public void save() {
System.out.println("BOOK dao running");
System.out.println("mybatis running"+arrayList);
System.out.println("Properties running"+properties);
for (int i = 0;i<arr.length;i++){
System.out.println(arr[i]);
}
System.out.println("HashSet"+hashSet);
System.out.println("hashMap"+hashMap);
System.out.println("Book资源保存中....");
}
}
在service层创建他的私有方法
package com.liuboss.service.Impl;
import com.liuboss.book.BookDao;
import com.liuboss.dao.UserDao;
import com.liuboss.service.UserService;
public class UserServiceImpl implements UserService {
private UserDao userDao;
private BookDao bookDao;
public void setBookDao(BookDao bookDao) {
this.bookDao = bookDao;
}
private String username;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public void setUsername(String username) {
this.username = username;
}
public void save() {
userDao.save();
bookDao.save();
System.out.println("Service---资源保存中................."+username);
}
}
配置进行属性注入
<?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="userService" scope="singleton" init-method="init" destroy-method="destory" class="com.liuboss.service.Impl.UsreServiceImpl">
<property name="userDao" ref="userDao"></property>
<property name="username" value="永乐大帝"/>
</bean>-->
<bean id="userDao" class="com.liuboss.dao.Impl.UserDaoImpl"/>
<bean id="userService" class="com.liuboss.service.Impl.UserServiceImpl">
<property name="userDao" ref="userDao"/>
<property name="bookDao" ref="bookDao"/>
</bean>
<bean id="bookDao" class="com.liuboss.book.Impl.BookDaoImpl">
<property name="properties">
<props>
<prop key="name">朱棣</prop>
<prop key="value">永乐大帝</prop>
</props>
</property>
<property name="arr">
<list>
<value>111</value>
<value>222</value>
</list>
</property>
<property name="hashMap">
<map>
<entry key="name" value="大秦始皇帝"/>
</map>
</property>
<property name="arrayList">
<list>
<value>朱元璋</value>
<value>朱棣</value>
</list>
</property>
<property name="hashSet">
<set>
<value>朱元璋</value>
<value>洪武大帝</value>
</set>
</property>
</bean>
</beans>
本文地址:https://blog.csdn.net/qq_39696115/article/details/110491812
上一篇: 回溯法求解K图染色问题(java版)
下一篇: Java线性分类器
推荐阅读
-
PHP 面向对象程序设计(oop)学习笔记(一) - 抽象类、对象接口、instanceof 和契约式编程
-
C#函数式编程中的惰性求值详解
-
ASP.NET防范SQL注入式攻击的方法
-
JS中的一些常用的函数式编程术语
-
PHP 面向对象程序设计(oop)学习笔记(一) - 抽象类、对象接口、instanceof 和契约式编程
-
PHP OPP机制和模式简介(抽象类、接口和契约式编程)
-
用python + hadoop streaming 分布式编程(一) -- 原理介绍,样例程序与本地调试
-
拿 C# 搞函数式编程 - 1
-
Python基础编程语法(for循环、推导式、函数)
-
SQL 注入式攻击的本质