一、Spring框架介紹
Spring優點:
1、方便解耦,簡化開發,IOC控制反轉
Spring 就是一個大工廠,可以將所有對象創建和依賴關系維護交給Spring
2、AOP 編程的支持
Spring 提供面向切編程,可以方便的實現對序進行權限攔截、運監控等功能
3、聲明式事務的支持(張三給李四轉賬,要么同時成功,要么同時失敗)
只需要通過配置就可以完成對事務的管理,而無手動編程
4、方便集成各種優秀框架
Spring 不排斥各種優秀的開源框架,其內部提供了對各種優優秀框架的支持(如Struts,Mybatis,Hibernate)。
SSH SSM
二、IOC和DI
控制反轉(Inversion on Control)IOC:對象的創建交給外部容器來完成(這里就是交給Spring容器),這就叫控制反轉。
IOC:Spring就是一個大的內存容器(一塊內存區域),三層架構里面上一層不需要再去new下一層對象,在上一層只需要寫下一層的接口,new對象由Spring容器幫我們完成,各層直接向Spring容器要對象就可以。
class StudentController{// 需要什么,就去創建什么(自己去new),這就叫“控制正轉”(通俗一點就是自己控制new哪個對象)private IStudentService studentService = new StudentServiceImpl();
}class StudentController{// 對象的創建交給別人去new(現在交給Spring容器new StudentServiceImpl(),new出來對方放在Spring容器),這就叫控制反轉“IOC”private IStudentService studentService; // 將Spring容器中new出來的對象通過set方法賦值給studentService,這個過程叫依賴注入:DIpublic void setStudentService(IStudentService studentService) {this.studentService = studentService;}
}
依賴注入:Dependency injection (DI)
現在new這個對象不是由自己new,是由Spring容器幫我們new對象,現在要得到這個Spring容器new出來的對象,就要“依賴”Spring容器,“注入”Spring容器new出來的對象。
IOC和DI區別:
IOC:解決對象創建的問題(對象的創建交給別人)Inversion of Control。
DI:在創建完對象后,對象關系的處理就是依賴注入(通過set方法實現依賴注入)Dependency injection。
先有IOC(對象創建),再有DI(處理對象關系)
在三層架構中最終實現的效果是:在Controller不會出現Service層具體的實現類代碼,只會看到Service層接口,Service層也不會出現Dao層具體實現類的代碼,也只會看到Dao層的接口
四、Bean的屬性: scope范圍
1、singleton
是scope的默認值,單例模式,在Spring容器中只存在一個實例。
public void test3() {ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");Student student1 = (Student) context.getBean("student");Student student2 = (Student) context.getBean("student");System.out.println(student1 == student2);// true
}
2、prototype
多例,會創建多個對象,每次去容器里面拿會創建一個新的實例
<bean scope="prototype" name="student" class="com.situ.spring.pojo.Student"/>public void test3() {ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");Student student1 = (Student) context.getBean("student");Student student2 = (Student) context.getBean("student");System.out.println(student1 == student2);// false
}
五、Spring屬性注入方式
1、set方法注入
<bean name="banji" class="com.situ.spring.pojo.Banji"><property name="id" value="1"/><property name="name" value="Java2023"/>
</bean>
<bean name="student" class="com.situ.spring.pojo.Student"><!-- 值類型注入 --><property name="id" value="1"/><property name="name" value="張三"/><property name="age" value="23"/><property name="gender" value="男"/><!-- ref:reference參考、引用引用類型的注入--><property name="banji" ref="banji"/>
</bean>
2、構造方法注入
argument:參數
parameter:參數
<bean name="banji" class="com.situ.spring.pojo.Banji"><constructor-arg name="id" value="1"/><constructor-arg name="name" value="Java2023"/>
</bean>
<bean name="student" class="com.situ.spring.pojo.Student"><constructor-arg name="id" value="1"/><constructor-arg name="name" value="李四"/><constructor-arg name="age" value="23"/><constructor-arg name="gender" value="男"/><constructor-arg name="banji" ref="banji"/>
</bean>
3、注解方式注入
@Resource @Autowired
六、三層架構使用Spring來管理
1、set方法注入
三層架構使用Spring來管理,達到一個目的:在上層只看到下一層的接口就可以,不需要出現具體的實現類。
使用set方式注入:
<bean name="studentDao" class="com.situ.spring.dao.impl.StudentDaoImpl"/>
<bean name="studentService" class="com.situ.spring.service.impl.StudentServiceImpl"><property name="studentDao" ref="studentDao"/>
</bean>
<bean name="studentController" class="com.situ.spring.controller.StudentController"><property name="studentService" ref="studentService"/>
</bean>
2、注解開發方式
<!--base-package:是要掃描的包,掃描這個包下面類上帶有注解@Controller @Service @Repositioy -->
<context:component-scan base-package="com.situ.spring"/>
@Controller @Service @Repository 本身沒有區別,都是new一個對象放到Spring容器中
@Component new一個對象放到Spring容器中,不起名字,放到容器中默認的名字是類名首個單詞首字母小寫
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Controller {/*** The value may indicate a suggestion for a logical component name,* to be turned into a Spring bean in case of an autodetected component.* @return the suggested component name, if any (or empty String otherwise)*/@AliasFor(annotation = Component.class)String value() default "";}
/*
<bean name="courseController" class="com.situ.spring.controller.CourseController">
</bean>
@Controller 這個注解相當于在applicationContext.xml中寫的上面的bean,
默認的名字是類名的首個單詞小寫courseController
*/
@Controller("courseController")
public class CourseController {// <property name="courseService" ref="courseService"/>// @Resource:從Spring容器中根據名字拿出指定的對象注入進來@Resource(name = "courseService")private ICourseService courseService;public void selectAll() {System.out.println("CourseController.selectAll()");courseService.selectAll();}
}/*
<bean name="courseService" class="com.situ.spring.service.impl.CourseServiceImpl">
</bean>
*/
@Service("courseService")
public class CourseServiceImpl implements ICourseService{//<property name="courseDao" ref="courseDao"/>@Resource(name = "courseDao")private ICourseDao courseDao;@Overridepublic void selectAll() {System.out.println("CourseServiceImpl.selectAll()");courseDao.selectAll();}
}// <bean name="courseDao" class="com.situ.spring.dao.impl.CourseDaoImpl"></bean>
@Repository("courseDao")
public class CourseDaoIml implements ICourseDao{@Overridepublic void selectAll() {System.out.println("CourseDaoIml.selectAll()");}
}
七、Autowired 自動裝配
1、@Autowired和@Resource區別
- @Resource默認是按照名稱裝配的,是JDK提供的。
- @Autowired是默認按照類型裝配的 ,是Spring提供的。
2、@Autowired一個接口有多個子類情況
@Service
public class BanjiServiceImpl implements IBanjiService {}@Service
public class BanjiServiceImpl2 implements IBanjiService{}

- 方法一:IBanjiService banjiServiceImpl2;
- 方法二:還是希望寫成IBanjiService banjiService,@Qualifier中限定名字
// @Resource(name = "banjiServiceImpl2")
@Autowired
@Qualifier(value = "banjiServiceImpl2")
private IBanjiService banjiService;
3、@Autowired注入bean
@Controller
public class StudentController {// <property name="studentService" ref="studentService"/>// @Resource(name = "studentService")@Autowiredprivate IStudentService studentService;}//@Service("studentService")
@Service
public class StudentServiceImpl implements IStudentService{// @Resource(name = "studentDao")@Autowiredprivate IStudentDao studentDao;}//@Repository("studentDao")
@Repository
public class StudentDaoImpl implements IStudentDao{}