링크: http://codeholic.net/post/39827697943/mac-os-x-redis

저작자 표시 비영리 변경 금지
신고
posted by 뚱2

설명보다 제목적기가 더 힘들다.


Bean을 일일이 설정하기 힘들기 때문에 SpringMVC에서는 MVC 구분에 맞춰서

@Controller, @Service, @Repository가 있다. 또한 이와 관계없이 @Component  어노테이션이 존재한다.


나는 보통 @Controller는 servlet context에 설정하고

@Service, @Repository, @Component는 root context에 설정한다.


그래서 servlet-context에 아래와 같이 설정했다.

    <context:component-scan base-package="com.ddoong2">

        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />

        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" />

        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository" />

    </context:component-scan>


그리고 root-context에는 다음과 같이 설정했다.

    <context:component-scan base-package="com.ddoong2">

        <context:include-filter type="annotation" expression="org.springframework.stereotype.Service" />

        <context:include-filter type="annotation" expression="org.springframework.stereotype.Repository" />

        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />

    </context:component-scan>



그런데 이번에 @Component 어노테이션을 사용할 일이 있어서 사용했더니 이개 두번 생성된다.

servlet context와 root context에서 같이 생성되는 현상이 발생했다.


그래서 servlet context를 아래와 같이 설정했다.

    <context:component-scan base-package="com.ddoong2">

        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />

        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" />

        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository" />

        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Component" />

    </context:component-scan>


그랬더니 페이지를 찾을 수 없다는 404에러가 발생했다.

원인은 컨트롤러 빈이 로딩이 되지 않는것이였다.

원인을 찾아보니 @Controller 어노테이션의 소스에서 찾았다.

@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

*/

String value() default "";


}

Controller 어노테이션이 Component 어노테이션을 사용하고 있는 것


그리고 최종으로 아래와 같이 수정해서 해결했다.

servlet context 

    <context:component-scan base-package="com.ddoong2" use-default-filters="false">

        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />

    </context:component-scan>


root context

    <context:component-scan base-package="com.ddoong2" use-default-filters="false">

        <context:include-filter type="annotation" expression="org.springframework.stereotype.Service" />

        <context:include-filter type="annotation" expression="org.springframework.stereotype.Repository" />

        <context:include-filter type="annotation" expression="org.springframework.stereotype.Component" />

    </context:component-scan>


component-scan의 프로퍼티중에 use-default-filters의 기본값은 true 이다.

이 부분을 false로 하고 설정을 하면 다른 필터는 로딩되지 않고 순수하게 설정된 부분만 필터링이된다.

저작자 표시 비영리 변경 금지
신고
posted by 뚱2

참고 : http://peterkellyonline.blogspot.kr/2011/04/configuring-windows-service.html

 

결국 어플리 케이션을 디테일하게 컨트롤 할려면 .Net에서 WinApi를 호출해야 한다는 결론

위 Url참고 하여 GetStartMode메소드 추가

 

    public static class ServiceHelper
    {
        [DllImport("advapi32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
        public static extern Boolean ChangeServiceConfig(
            IntPtr hService,
            UInt32 nServiceType,
            UInt32 nStartType,
            UInt32 nErrorControl,
            String lpBinaryPathName,
            String lpLoadOrderGroup,
            IntPtr lpdwTagId,
            [In] char[] lpDependencies,
            String lpServiceStartName,
            String lpPassword,
            String lpDisplayName);

        [DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        static extern IntPtr OpenService(
            IntPtr hSCManager, string lpServiceName, uint dwDesiredAccess);

        [DllImport("advapi32.dll", EntryPoint = "OpenSCManagerW", ExactSpelling = true, CharSet = CharSet.Unicode, SetLastError = true)]
        public static extern IntPtr OpenSCManager(
            string machineName, string databaseName, uint dwAccess);

        [DllImport("advapi32.dll", EntryPoint = "CloseServiceHandle")]
        public static extern int CloseServiceHandle(IntPtr hSCObject);

        [DllImport("advapi32.dll", CharSet=CharSet.Unicode, SetLastError=true)]
        public static extern Boolean QueryServiceConfig(IntPtr hService, IntPtr intPtrQueryConfig, UInt32 cbBufSize, out UInt32 pcbBytesNeeded);

        [DllImport( "advapi32.dll", CharSet = CharSet.Unicode, SetLastError = true, EntryPoint = "QueryServiceConfig2W" )]
        public static extern Boolean QueryServiceConfig2( IntPtr hService, UInt32 dwInfoLevel, IntPtr buffer, UInt32 cbBufSize, out UInt32 pcbBytesNeeded );


        private const uint SERVICE_NO_CHANGE     = 0xFFFFFFFF;
        private const uint SERVICE_QUERY_CONFIG  = 0x00000001;
        private const uint SERVICE_CHANGE_CONFIG = 0x00000002;
        private const uint SC_MANAGER_ALL_ACCESS = 0x000F003F;

        [StructLayout(LayoutKind.Sequential)]
        public class QUERY_SERVICE_CONFIG
        {
            [MarshalAs(System.Runtime.InteropServices.UnmanagedType.U4)]
            public UInt32 dwServiceType;
            [MarshalAs(System.Runtime.InteropServices.UnmanagedType.U4)]
            public UInt32 dwStartType;
            [MarshalAs(System.Runtime.InteropServices.UnmanagedType.U4)]
            public UInt32 dwErrorControl;
            [MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPWStr)]
            public String lpBinaryPathName;
            [MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPWStr)]
            public String lpLoadOrderGroup;
            [MarshalAs(System.Runtime.InteropServices.UnmanagedType.U4)]
            public UInt32 dwTagID;
            [MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPWStr)]
            public String lpDependencies;
            [MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPWStr)]
            public String lpServiceStartName;
            [MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPWStr)]
            public String lpDisplayName;
        };

        public static void ChangeStartMode(ServiceController svc, ServiceStartMode mode)
        {
            var scManagerHandle = OpenSCManager(null, null, SC_MANAGER_ALL_ACCESS);
            if (scManagerHandle == IntPtr.Zero)
            {
                throw new ExternalException("Open Service Manager Error");
            }

            var serviceHandle = OpenService(
                scManagerHandle,
                svc.ServiceName,
                SERVICE_QUERY_CONFIG | SERVICE_CHANGE_CONFIG);

            if (serviceHandle == IntPtr.Zero)
            {
                throw new ExternalException("Open Service Error");
            }

            var result = ChangeServiceConfig(
                serviceHandle,
                SERVICE_NO_CHANGE,
                (uint)mode,
                SERVICE_NO_CHANGE,
                null,
                null,
                IntPtr.Zero,
                null,
                null,
                null,
                null);

            if (result == false)
            {
                int nError = Marshal.GetLastWin32Error();
                var win32Exception = new Win32Exception(nError);
                throw new ExternalException("Could not change service start type: "
                    + win32Exception.Message);
            }

            CloseServiceHandle(serviceHandle);
            CloseServiceHandle(scManagerHandle);
        }

        /// <summary>
        /// Automatic = 2, Manual = 3, Disabled = 4
        /// </summary>
        /// <param name="svc"></param>
        /// <returns></returns>
        public static uint GetStartMode(ServiceController svc)
        {
            var scManagerHandle = OpenSCManager(null, null, SC_MANAGER_ALL_ACCESS);
            if (scManagerHandle == IntPtr.Zero)
            {
                throw new ExternalException("Open Service Manager Error");
            }

            var serviceHandle = OpenService(
                scManagerHandle,
                svc.ServiceName,
                SERVICE_QUERY_CONFIG);

            if (serviceHandle == IntPtr.Zero)
            {
                throw new ExternalException("Open Service Error");
            }

            UInt32 dwBytesNeeded = 0;
            IntPtr ptr = Marshal.AllocHGlobal(4096);// Allocate memory for struct.

            bool result = QueryServiceConfig(
                serviceHandle,
                ptr,
                4096,
                out dwBytesNeeded);

            QUERY_SERVICE_CONFIG qUERY_SERVICE_CONFIG = new QUERY_SERVICE_CONFIG();
            Marshal.PtrToStructure(ptr, qUERY_SERVICE_CONFIG);// Copy;
            Marshal.FreeHGlobal(ptr);

            if (result == false)
            {
                int nError = Marshal.GetLastWin32Error();
                var win32Exception = new Win32Exception(nError);
                throw new ExternalException("Could not QueryServiceConfig : "
                    + win32Exception.Message);
            }

            CloseServiceHandle(serviceHandle);
            CloseServiceHandle(scManagerHandle);

            return qUERY_SERVICE_CONFIG.dwStartType;
        }
    }

 

var svc = new ServiceController("BITS");
ServiceHelper.ChangeStartMode(svc, ServiceStartMode.Automatic);

// You can then Start the service if necessary.
if (svc.Status != ServiceControllerStatus.Running)
{
   svc.Start();
}

// And of course you should close the service when no longer needed
svc.Close();

저작자 표시 비영리 변경 금지
신고

'.Net > .Net' 카테고리의 다른 글

[.Net] TransactedInstaller  (0) 2013.04.22
[.Net] Windows Service Current Directory  (0) 2013.04.20
[.Net] Changing Start Mode of a Windows Service  (0) 2013.04.20
[.Net] Mutex 클래스  (0) 2013.04.19
[.Net] ServiceController  (0) 2013.04.18
[.Net] Windows 서비스 만들기  (0) 2013.04.17
posted by 뚱2

* 연습 : 구성 요소 디자이너에서 Windows 서비스 응용 프로그램 만들기

링크 : http://msdn.microsoft.com/ko-kr/library/zt39148a(VS.80).aspx


* 방법 : Windows 서비스 응용 프로그램 디버깅

링크 : http://msdn.microsoft.com/ko-kr/library/7a50syb3(VS.80).aspx


저작자 표시 비영리 변경 금지
신고

'.Net > .Net' 카테고리의 다른 글

[.Net] Mutex 클래스  (0) 2013.04.19
[.Net] ServiceController  (0) 2013.04.18
[.Net] Windows 서비스 만들기  (0) 2013.04.17
[.Net] HTTP POST/WebClient (C#) and CSV formated string  (0) 2013.04.12
[.Net] RSA 암호화  (0) 2013.03.13
[.Net] EXE를 포함한 외부 DLL을 같이 배포하기  (0) 2013.02.19
posted by 뚱2

링크 : http://suminpapa.tistory.com/107 





WindowsService.zip


저작자 표시 비영리 변경 금지
신고
posted by 뚱2