Skip to content

Instantly share code, notes, and snippets.

@JavaNoobPig
JavaNoobPig / IntroductionSpring17_6.xml
Created August 14, 2018 07:57
IntroductionSpring17_6
<?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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.pig.spring.basics" />
@JavaNoobPig
JavaNoobPig / IntroductionSpring18_1.java
Created February 23, 2019 06:21
IntroductionSpring18_1
package com.pig.spring.basics.springin5steps.Scope;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class PersonDAO {
@Autowired
JdbcConnection jdbcConnection;
@JavaNoobPig
JavaNoobPig / IntroductionSpring18_2.java
Last active February 23, 2019 06:26
IntroductionSpring18_2
package com.pig.spring.basics.springin5steps.Scope;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
@Repository //<--改這裡
public class PersonDAO {
@Autowired
JdbcConnection jdbcConnection;
@JavaNoobPig
JavaNoobPig / IntroductionSpring18_3.java
Created February 23, 2019 06:31
IntroductionSpring18_3
package com.pig.spring.basics.componentscan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
@Repository //<--改這裡
public class ComponentDAO {
@Autowired
ComponentJdbcConnection jdbcConnection;
@JavaNoobPig
JavaNoobPig / IntroductionSpring18_4.java
Created February 23, 2019 06:42
IntroductionSpring18_4
package com.pig.spring.basics.springin5steps.Basic;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
@JavaNoobPig
JavaNoobPig / IntroductionSpring18_5.java
Created February 23, 2019 06:43
IntroductionSpring18_5
package com.pig.spring.basics.springin5steps.Basic;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
@Service //<-改這裡
@Qualifier("bubble")
public class BubbleSortAlgorithm implements SortAlgorithm{
public int[] sort(int[] numbers) {
// Logic for Bubble Sort
@JavaNoobPig
JavaNoobPig / IntroductionSpring18_6.java
Created February 23, 2019 06:45
IntroductionSpring18_6
package com.pig.spring.basics.springin5steps.Basic;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
@Service //<-改這裡
@Qualifier("quick")
public class QuickSortAlgorithm implements SortAlgorithm{
public int[] sort(int[] numbers) {
@JavaNoobPig
JavaNoobPig / IntroductionSpring19_1.java
Last active February 23, 2019 07:42
IntroductionSpring19_1
package com.pig.spring.basics.springin5steps.properties;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component //<-要讓Spring找到所以要有宣告是Component
public class SomeExternalService {
@Value("${erternal.service.url}") //<-預計在設定檔內的key值
private String url;
@JavaNoobPig
JavaNoobPig / IntroductionSpring19_2.properties
Created February 23, 2019 07:28
IntroductionSpring19_2
erternal.service.url=http://someserver.dev.com/service
@JavaNoobPig
JavaNoobPig / IntroductionSpring19_3.java
Last active February 23, 2019 07:41
IntroductionSpring19_3
package com.pig.spring.basics.springin5steps;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import com.pig.spring.basics.springin5steps.properties.SomeExternalService;
@Configuration